Section 7.5. Operator Precedence

   

7.5 Operator Precedence

The compiler must know the order in which to evaluate a series of operators. For example, if I write:

 myVariable = 5 + 7 * 3 

there are three operators for the compiler to evaluate ( = , + , and * ). It could, for example, operate left to right, which would assign the value 5 to myVariable, then add 7 to the 5 (12) and multiply by 3 (36). Since we're evaluating from left to right, the assignment has been done, so the value 36 is thrown away. This is clearly not what is intended.

The rules of precedence tell the compiler which operators to evaluate first. As is the case in algebra, multiplication has higher precedence than addition, so 5+7*3 is equal to 26 rather than 36. Both addition and multiplication have higher precedence than assignment, so the compiler will do the math, and then assign the result (26) to myVariable only after the math is completed.

In VB.NET, parentheses are also used to change the order of precedence much as they are in algebra. Thus, you can change the result by writing:

 myVariable = (5+7) * 3 

Grouping the elements of the assignment in this way causes the compiler to add 5+7, multiply the result by 3, and then assign that value (36) to myVariable.

Within a single line of code, operators are evaluated in the following order:

  • Mathematical

  • Concatenation

  • Relational/Comparison

  • Logical

Relational operators are evaluated left to right. Mathematical operators are evaluated in this order:

  • Exponentiation ( ^ )

  • Division and multiplication ( / , * )

  • Integer division ( \ )

  • Modulus operator ( Mod )

  • Addition and subtraction ( + , - )

The logical operators are evaluated in this order:

  • Not

  • And

  • Or

  • XOr

In some complex equations, you might need to nest parentheses to ensure the proper order of operations. For example, assume I want to know how many seconds my family wastes each morning. It turns out that the adults spend 20 minutes over coffee each morning and 10 minutes reading the newspaper. The children waste 30 minutes dawdling and 10 minutes arguing.

Here's my algorithm:

 (((minDrinkingCoffee  + minReadingNewspaper )* numAdults ) +  ((minDawdling + minArguing) * numChildren)) * secondsPerMinute. 

Although this works, it is hard to read and hard to get right. It's much easier to use interim variables :

 wastedByEachAdult = minDrinkingCoffee  +  minReadingNewspaper wastedByAllAdults =  wastedByEachAdult * numAdults wastedByEachKid =  minDawdling  + minArguing wastedByAllKids =  wastedByEachKid * numChildren wastedByFamily = wastedByAllAdults + wastedByAllKids totalSeconds =  wastedByFamily * 60 

The latter example uses many more interim variables, but it is far easier to read, understand, and (most important) debug. As you step through this program in your debugger, you can see the interim values and make sure they are correct. See Chapter 10 for more information.

A more complete listing is shown in Example 7-5.

Example 7-5. Using parentheses and interim variables
 Option Strict On Imports System Module Module1    Sub Main( )       Dim minDrinkingCoffee As Integer = 5       Dim minReadingNewspaper As Integer = 10       Dim minArguing As Integer = 15       Dim minDawdling As Integer = 20       Dim numAdults As Integer = 2       Dim numChildren As Integer = 2       Dim wastedByEachAdult As Integer       Dim wastedByAllAdults As Integer       Dim wastedByEachKid As Integer       Dim wastedByAllKids As Integer       Dim wastedByFamily As Integer       Dim totalSeconds As Integer       wastedByEachAdult = minDrinkingCoffee + minReadingNewspaper       wastedByAllAdults = wastedByEachAdult * numAdults       wastedByEachKid = minDawdling + minArguing       wastedByAllKids = wastedByEachKid * numChildren       wastedByFamily = wastedByAllAdults + wastedByAllKids       totalSeconds = wastedByFamily * 60       Console.WriteLine("Each adult wastes {0} minutes", wastedByEachAdult)       Console.WriteLine("Each child wastes {0} mintues", wastedByEachKid)       Console.WriteLine("Total minutes wasted by entire family: {0}", _                wastedByFamily)       Console.WriteLine("Total wasted seconds: {0}", totalSeconds)    End Sub ' End of Main( ) module definition  End Module 
  Output:  Each adult wastes 15 minutes Each child wasts 35 mintues Total minutes wasted by entire family: 100 Total wasted seconds: 6000 
   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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