Arithmetic: Creating Expressions with Variables and Operators

In this lesson, you will learn how to create expressions to perform arithmetic and then return values.

An expression is a segment of code that performs arithmetic and then returns a value. For example, a simple addition expression is shown here:

5 + 4

The expression 5 + 4 returns the value 9 when evaluated and consists of two parts: the operands(5 and 4), which are the values that the operation is performed on, and the operator (), which specifies the operation to be performed.

Using Values Returned by Expressions

For an expression to be useful, you must do something with the value that is returned. The most common thing to do is to assign it to a variable, as shown here:

Dim anInteger As Integer = 5 + 4

This example declares a new Integer variable called anInteger and assigns the value returned by 5 + 4 to it.

Arithmetic Operators

A common use for expressions is to perform arithmetic on variables: addition, subtraction, multiplication, or division. The following table describes the operators frequently used for arithmetic.

Operator

Description

Example

+ (addition)

Returns the sum of two operands

5 + 4

- (subtraction)

Returns the difference of two operands

5 - 4

* (multiplication)

Returns the product of two operands

5 * 4

/ (division)

Returns the quotient of two operands

5 / 4

The kind of variable that you use when you perform arithmetic can affect the result. Dividing two numbers often results in a return value that is not a whole number. For example, when you divide 3 by 2, the result is 1.5. If you assigned the return value of that expression to an Integer variable, it would be rounded to the nearest whole number, 2. When performing division, you should use a Double variable to store the return value.

Note

You can also convert a variable from one data type to another by using the conversion functions of Visual Basic. For more information, see Closer Look: Converting from One Variable Type to Another.

Try It!

To add numbers

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type Arithmetic and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag two Textbox controls onto the form.

  5. From the Toolbox, drag a Button control onto the form.

  6. Double-click the Button to open the Code Editor.

  7. In the Button1_Click event procedure, type the following code.

    Dim A As Double = Textbox1.Text
    Dim B As Double = Textbox2.Text
    
    MsgBox(A + B)
    MsgBox(A - B)
    MsgBox(A * B)
    MsgBox(A / B)
    

    The first two lines declare the variables A and B. A and B will hold the numeric values used in this program and assign the values of the two TextBox controls (their text) to the variables A and B.

    The final four lines create expressions with the two variables and each of the basic arithmetic operators, and display the results of those expressions in a message box.

  8. Press F5 to run the application.

  9. Type a number in each text box and click Button1.

    Note

    If you type any other character into the text boxes, an error will occur.

    Expressions are created by using the two numbers that you enter and each of the four basic arithmetic operators (addition, subtraction, multiplication, and division). The result of each expression is displayed in a message box.

Next Steps

In this lesson, you learned about how to create and use expressions. You also learned about operands and operators, and how to construct an expression. At this point, you can either continue to the next lesson, Comparisons: Using Expressions to Compare Values, or learn more about how to convert variables to different types in Closer Look: Converting from One Variable Type to Another.

See Also

Tasks

Closer Look: Converting from One Variable Type to Another

Concepts

Arithmetic Operators in Visual Basic