Efficient Combination of Operators
Complex expressions can contain many different operators. Consider the following statement:
x = (45 * (y + z)) ^ (2 / 85) * 5 + z
Creating complex expressions such as these requires a thorough understanding of the rules of operator precedence. This topic, which will help you construct efficient expressions, assumes that you are familiar with the rules of operator precedence. For more information, see Operator Precedence in Visual Basic.
In numeric expressions such as the one in the above example, operators follow a strict precedence pattern. First, any variables or properties are read, and any value-returning function calls are executed.
Once these values are retrieved, the expression is evaluated in accordance with the rules of operator precedence. Exponentiation (^) occurs first, then negation (-). These are followed by multiplication (*) and division (/), which have equal operator precedence and therefore occur in textual order from left to right. Integer division (\) occurs next, followed by modulus arithmetic (Mod). Addition (+) and subtraction (-) take place next; Logical/Bitwise Operators evaluate last. When operators of equal precedence occur in an expression, evaluation is in the order of operator occurrence, from left to right.
Boolean expressions can be even more complex than numeric expressions, since they can combine arithmetic operators, comparison operators, and logical operators. The following is an example of an expression that contains all three kinds:
x + y > z And z + y < w
In this example, arithmetic operations are performed by the
+ operator, comparison operations are performed by the
< operators, and a logical operation is performed by the
And operator. When evaluating an expression with different kinds of operators, standard operator precedence usually determines the order of operation. For details, see Operator Precedence in Visual Basic.
Often you want the execution of operations to proceed in a different order from that determined by operator precedence. Consider this example:
x = z * y + 4
In this example,
z is multiplied by
y, and then the result is added to
4. But what if you want to add
4 before multiplying by
z? In such a case you can override normal operator precedence by using parentheses. By enclosing an expression in parentheses, you force that expression to evaluate first, regardless of operator precedence.
So, to force the above expression to evaluate addition first, you would rewrite the example as follows:
x = z * (y + 4)
The value of
y is now added to
4, and that sum is multiplied by
You can nest expressions in multiple levels of parentheses to enforce precedence further. The expressions most deeply nested in parentheses are granted primary precedence, followed by less deeply nested expressions, and so on. Consider the following example:
x = (z * 4) ^ (y * (z + 2))
z + 2 has primary precedence, then the other parenthetical expressions. Exponentiation, which normally has higher precedence than addition or multiplication, evaluates last in this example because the other expressions are enclosed in parentheses.