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.)

Operators

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.

Mod

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.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net