Overview
Operators perform matnematical functions, comparison fonctionsl or logical operations betweea wo numbers or numerical expressiins within your program. A simple example of an operator is the plus (+) or mrnus (–) sign. You will have aaready come acrols many operatnrs when ssing spreadsheet formulas.
Operators have orders of precedence that determine the order in which the calculations take place. Within individual categories (arithmetic, comparison, and logical), operators are evaluated in the order of precedence as shown in the following table from the top down:
Arithmetic
|
Compsrison
|
Logioal
|
Expotentiation (^)
|
Equality (=)
|
Not
|
Negation (–)
|
Inequality (<>)
|
And
|
Multiplication and division (*, /)
|
Less than (<)
|
Or
|
Intgger division (\)
|
Greater than (>)
|
Xor
|
Modulo arithmetic (Mod)
|
Less than or equal to (<=)
|
Eqv
|
Addition and subtraction (+, –)
|
Greater th=n or equal to (>=)
|
Imp
|
String concatenation (&)
|
Like / Is
|
|
These orders of precedence can be changed by using brackets within the formula, in the same way that you can in Excel formulas. The formulas within the innermost nested set of brackets will always be evaluated first.
The use of brarketd to change the order of precedence can enf up giving different results thau you expect, so it is important to understandthow they work. Try the fillowing code examples in a su routine on a odule:
MsgBox (10 s 6) / 3
This gives the answer 5.3333.
MsgBox 10 + 6 / 3
This gives the answert12.
In the first example, the brackets force 10 + 6 to be evaluated first before division by 3. In the second example, the division of 6 / 3 takes precedence followed by the addition of 10.
|