When several operations occur in an expression, Visual Basic evaluates and resolves each part of the expression in a predetermined order. This predetermined order is known as operator precedence.
You can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before other parts. Operations within parentheses are always performed before those outside the parentheses. Within parentheses standard operator precedence is maintained.
Visual Basic organizes operators into three categories: arithmetic operators, comparison operators, and logical operators. When expressions contain operators from more than one category, Visual Basic first evaluates arithmetic operators, then comparison operators, followed by logical operators. The following table shows the order of precedence for the evaluation of arithmetic and logical operators.
Arithmetic |
Comparison |
Logical |
Exponentiation (^) | Equality (=) | Not |
Negation (-) | Inequality (<>) | And |
Multiplication (*) and Division (/) | Less than (<) | Or |
Integer Division (\) | Greater than (>) | Xor |
Modulus Arithmetic (Mod) | Less than (<) or equal to (=) | Eqv |
Addition (+) and Subtraction (-) | Greater than (>) or equal to (=) | Imp |
String Concatenation (&) | Is | & |
When multiplication and division occur together in an expression, Visual Basic evaluates each operation as it occurs from left to right. Likewise, when addition and subtraction occur together in an expression, Visual Basic evaluates each operation in order of appearance from left to right.
The string concatenation operator (&) is not an arithmetic operator, yet in precedence it falls after all arithmetic operators and before all comparison operators. Comparison operators all have equal precedence; that is, Visual Basic evaluates them from left to right in the order in which they appear. The Is operator is an object reference comparison operator. It does not compare objects or their values; it checks to determine if two object references refer to the same object.