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 below.
5 + 4
The expression 5 + 4 returns the value 9 when evaluated and is made up 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 below.
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.
A common use for expressions is to perform arithmetic on variables: addition, subtraction, multiplication, or division. The following table describes the operators commonly 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 performing 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.
To add numbers
-
On the File menu, choose 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.
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, which 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 of the text boxes and click Button1.
Note |
|---|
| If you type any other character into the text boxes, an error will occur. |
Expressions are created 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 topic, you learned about creating and using 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 converting variables to different types in Closer Look: Converting from One Variable Type to Another. If you choose "Closer Look," you will complete it and then proceed to the next lesson.
Tasks
Closer Look: Converting from One Variable Type to Another
Concepts
Arithmetic Operators in Visual Basic