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.
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:
This example declares a new Integer variable called anInteger and assigns the value returned by 5 + 4 to it.
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.
Returns the sum of two operands
5 + 4
Returns the difference of two operands
5 - 4
Returns the product of two operands
5 * 4
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.
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.
To add numbers
On the File menu, click New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type Arithmetic and then click OK.
A new Windows Forms project opens.
From the Toolbox, drag two Textbox controls onto the form.
From the Toolbox, drag a Button control onto the form.
Double-click the Button to open the Code Editor.
In the Button1_Click event procedure, type the following code.
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.
Press F5 to run the application.
Type a number in each text box and click Button1.
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.
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.