28 of 158
22 Operator Precedence Precedence Level Operators 11 (highest) ^ 10 +(unary) -(unary) 9 * / 8 \ 7 Mod 6 + - 5 & 4 = < > <= >= <> 3 Not 2 And 1 Or 0 (lowest) Xor The actual precedence level values are of no particular significance. All that matters is whether the precedence value of one operator is higher, equal to or lower than that of another operator. In the example used above, since the multiplication operator has higher precedence than the addition operator, the meaning of the expression is to multiply the value of the variable b by 5 and then add 3 to the result. It happens in this case that the order of application of the operators is left to right but that is not always the case. Consider this slightly different example: b = b + 5 * 3 The meaning of this expression is to multiply 5 by 3 and then add the value of the variable b to the result. The relative precedence level of the operators requires that they be applied in an order that is not left to right. BasicX Compatibility Note In BasicX mode, a different operator precedence set is used, as shown in the table below, in order to be compatible with BasicX. Operator Precedence Precedence Level Operators 5 (highest) ^ 4 +(unary) -(unary) 3 Not 2 * / \ Mod And 1 + - Or Xor & 0 (lowest) = < > <= >= <> 2.4.2 Operator Associativity After studying the precedence table in the preceding section, the obvious question would be what happens when two operators have the same precedence? b = b - 5 + 3 In case of equal precedence, the operators are applied in the order dictated by the associativity of the operator. Except for the exponentiation operator, all operators in ZBasic are left associative. This means that given equal precedence operators they are applied in left to right order. Exponentiation is right associative meaning that they will be applied in right to left order. However, no matter what the precedence levels are you can force the operators to be applied in any order that you wish by utilizing parentheses. Consider the two examples below.