One Step Further: Establishing Order of Precedence

One Step Further: Establishing Order of Precedence

In the previous few exercises, you experimented with several mathematical operators and one string operator. Visual Basic lets you mix as many mathematical operators as you like in a formula, as long as each numeric variable and expression is separated from another by one operator. For example, this is an acceptable Visual Basic formula:

Total = 10 + 15 * 2 / 4 ^ 2

The formula processes several values and assigns the result to a variable named Total. But how is such an expression evaluated by Visual Basic? In other words, what sequence does Visual Basic follow when solving the formula? You might not have noticed, but the order of evaluation matters a great deal in this example.

Visual Basic solves this dilemma by establishing a specific order of precedence for mathematical operations. This list of rules tells Visual Basic which operator to use first, second, and so on when evaluating an expression that contains more than one operator.

The following table lists the operators from first to last in the order in which they are evaluated. (Operators on the same level in this table are evaluated from left to right as they appear in an expression.)


Order of Precedence

( )

Values within parentheses are always evaluated first.


Exponentiation (raising a number to a power) is second.

Negation (creating a negative number) is third.

* /

Multiplication and division are fourth.


Integer division is fifth.


Remainder division is sixth.

+ -

Addition and subtraction are last.

Given the order of precedence in this table, the expression

Total = 10 + 15 * 2 / 4 ^ 2

is evaluated by Visual Basic in the following steps. (Bold type is used to show each step in the order of evaluation and its result.)

Total = 10 + 15 * 2 / 4 ^ 2
Total = 10 + 15 * 2/ 16 
Total = 10 + 30 / 16
Total = 10 + 1.875
Total = 11.875

Using Parentheses in a Formula

You can use one or more pairs of parentheses in a formula to clarify the order of precedence. For example, Visual Basic calculates the formula

Number = (8 - 5 * 3) ^ 2

by determining the value within the parentheses (-7) before doing the exponentiation—even though exponentiation is higher in order of precedence than subtraction and multiplication, according to the preceding table. You can further refine the calculation by placing nested parentheses in the formula. For example,

Number = ((8 - 5) * 3) ^ 2

directs Visual Basic to calculate the difference in the inner set of parentheses first, perform the operation in the outer parentheses next, and then determine the exponentiation. The result produced by the two formulas is different: the first formula evaluates to 49 and the second to 81. Parentheses can change the result of a mathematical operation, as well as make it easier to read.

Chapter 5 Quick Reference


Do this

Declare a variable

Type Dim followed by the variable name, the As keyword, and the variable data type in the program code. To make the variable valid in all of a form's event procedures, place this statement at the top of the code for the form, before any event procedures. For example:

Dim Country As String

Change the value of a variable

Assign a new value with the assignment operator of (=). For example:

Country = "Japan"

Get input with a dialog box

Use the InputBox function, and assign the result to a variable. For example:

UserName = InputBox("What is your name?")

Display output in a dialog box

Use the MsgBox function. (The string to be displayed in the dialog box can be stored in a variable.) For example:

Forecast = "Rain, mainly on the plain."
MsgBox(Forecast, , "Spain Weather Report")

Create a constant

Type the Const keyword followed by the constant name, the assignment operator (=), the constant data type, and the fixed value. For example:

Const JackBennysAge As Short = 39

Create a formula

Link together numeric variables or values with one of the seven mathematical operators, and then assign the result to a variable or a property. For example:

Result = 1 ^ 2 * 3 \ 4 'this equals 0

Combine text strings

Use the string concatenation operator (&). For example:

Msg = "Hello" & "," & " world!"

Include a class library from the .NET Framework

Place an Imports statement at the very top of the form's code that identifies the class library. For example:

Imports System.Math

Make a call to a method from an included class library

Use the method name, and include any necessary arguments so that it can be used in a formula or a program statement. For example, to make a call to the Sqrt method in the System.Math class library:

Hypotenuse = Sqrt(x ^ 2 + y ^ 2)

Control the evaluation order in a formula

Use parentheses in the formula. For example:

Result = 1 + 2 ^ 3 \ 4       'this equals 3 
Result = (1 + 2) ^ ( 3 \ 4)  'this equals 1