OPERATOR PRECEDENCE

When Visual Basic evaluates a complex expression, it must decide the order in which to evaluate operators. For example, consider the expression 1 + 2 * 3 / 4 + 2. The following text shows three orders in which you might evaluate this expression to get three different results:

1 + (2 * 3) / (4 + 2) = 1 + 6 / 6 = 2
1 + (2 * 3 / 4) + 2 = 1 + 1.5 + 2 = 4.5
(1 + 2) * 3 / (4 + 2) = 3 * 3 / 6 = 1.5
 

Precedence determines which operator Visual Basic executes first. For example, the Visual Basic precedence rules say the program should evaluate multiplication and division before addition, so the second equation is correct.

The following table lists the operators in order of precedence. When evaluating an expression, the program evaluates an operator before it evaluates those lower than it in the list.

OPERATOR DESCRIPTION
( ) Grouping (parentheses)
^ Exponentiation
- Negation
*, / Multiplication and division
\ Integer division
Mod Modulus
+, -, + Addition, subtraction, and concatenation
& Concatenation
<<, >> Bit shift
=, <>, <, <=, >, >=, Like, Is, IsNot, TypeOf ... Is All comparisons
Not Logical and bitwise negation
And, AndAlso Logical and bitwise And with and without short-circuit evaluation
Xor, Or, OrElse Logical and bitwise Xor, and Or with and without short-circuit evaluation

When operators are on the same line in the table, or if an expression contains more than one instance of the same operator, the program evaluates them in ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.