JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Understanding Visual Basic Operator Precedence

You've done well in your computer class-so well that the instructor has asked you to calculate the average grade on the final. Nothing could be easier, you think, and you put together the following program:

Module Module1
    Sub Main()
        Dim intGrade1, intGrade2, intGrade3, intNumberStudents As Integer
        intGrade1 = 60
        intGrade2 = 70
        intGrade3 = 80
        intNumberStudents = 3
        System.Console.WriteLine("Average grade = " & _
        Str(intGrade1 + intGrade2 + intGrade3 / intNumberStudents))
    End Sub
End Module

But when you run the program, it calmly informs you that the average score is 156.66666667. That doesn't look so good-what's wrong? The problem lies in this line:

Module Module1
    Sub Main()
        Dim intGrade1, intGrade2, intGrade3, intNumberStudents As Integer
        intGrade1 = 60
        intGrade2 = 70
        intGrade3 = 80
        intNumberStudents = 3
        System.Console.WriteLine("Average grade = " & _
        Str(intGrade1 + intGrade2 + intGrade3 / intNumberStudents))
    End Sub
End Module

Visual Basic evaluates the expression in parentheses from left to right, using pairs of operands and their associated operator, so it adds the first two grades together first. Instead of adding the final grade, however, it first divides that grade by NumberStudents, because the division operation has higher precedence than addition, so the result is 60 + 70 + (80/3) = 156.66666667.

The solution here is to group the values to add together this way using parentheses:

Module Module1
    Sub Main()
        Dim intGrade1, intGrade2, intGrade3, intNumberStudents As Integer
        intGrade1 = 60
        intGrade2 = 70
        intGrade3 = 80
        intNumberStudents = 3
        System.Console.WriteLine("Average grade = " & _
        Str((intGrade1 + intGrade2 + intGrade3) / intNumberStudents))
    End Sub
End Module

Running this new code gives us an average of 70, as it should. This example points out the need to understand how Visual Basic evaluates expressions involving operators. In general, such expressions are evaluated left to right, and when it comes to a contest between two operators (such as + and / in the last term of our original program above), the operator with the higher precedence is used first. When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence.

When expressions contain operators from more than one category, they are evaluated according to the following rules. The arithmetic and concatenation operators have an order of precedence that is described below, and all have higher precedence than the comparison and logical operators. Comparison operators have higher precedence than the logical operators, but lower precedence than the arithmetic and concatenation operators. All comparison operators have equal precedence; that is, they are evaluated in the order, left to right, in which they appear.

The Arithmetic operators have the highest precedence and are arranged this way, from highest precedence to lowest:

  • Exponentiation (^)

  • Negation (-) (for example, -intValue reverses the sign of the value in intValue)

  • Multiplication and division (*, /)

  • Integer division (\)

  • Modulus arithmetic (Mod)

  • Addition and subtraction (+,-)

Next come the Concatenation operators:

  • String concatenation (+)

  • String concatenation (&)

Next come the Comparison operators, which all have the same precedence and are evaluated from left to right as Visual Basic encounters them:

  • Equality (=)

  • Inequality (<>)

  • Less than, greater than (<,>)

  • Greater than or equal to (>=)

  • Less than or equal to (<=)

  • Like

  • Is

Finally come the Logical/Bitwise operators, which have this precedence order, from highest to lowest:

  • Negation-(Not)

  • Conjunction-(And,AndAlso)

  • Disjunction-(Or, OrElse, Xor)

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor 
divorce lawyer Edmonton . St. Louis Flooring Contractors