30 of 172
ZBasic Language Reference 23 ZX Microcontroller Family In this assignment statement, the value being assigned to the variable b is an expression comprising three operands and two operators. Some operators, like both of those in the expression above, are called “binary operators” because they require two operands. Other operators require only one operand and are called “unary operators”. The available operators and their characteristics are described in subsequent sections. ZBasic is a strongly typed language. This means that operands supplied for binary operators must generally be of the same type. The only exception to this rule is the exponentiation operator which allows a restricted mixing of types as described in Section 2.4.3. The order of evaluation of the components of an expression is governed by operator precedence and associativity as described in the next two sections. However, that order may be overridden by the use of parentheses. 2.4.1 Operator Precedence Consider again the example expression b = b * 5 + 3. Each of the operators requires two operands but it may not be immediately clear what the operands are in each case. It could be that the expression above means to add 5 to 3 and then multiply the result by the value of b. On the other hand, it could mean to multiply the value of b by 5 and then add 3 to the result. The property that dictates the order in which operators are applied in this case is called “operator precedence”. An operator having higher precedence has priority over an operator with lower precedence and is therefore applied first. The table below depicts the precedence of ZBasic operators. 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.