Operators are used in expressions to perform specific actions on one or more operands, generating a single return value or result. Operands are simply the values or expressions that each operator operates on. For instance, in the expression 1 + 2, the values "1" and "2" are the operands, and "+" is the symbol for the add operator.
Operators are said to have precedence, which is a way of describing the rules that determine which operations in a series of sub-expressions get performed first. A simple example would be the expression 1 + 2 * 3. In Setup Factory, the multiply (*) operator has higher precedence than the add (+) operator, so this expression is equivalent to 1 + (2 * 3). In other words, the sub-expression 2 * 3 is performed first, and then 1 + 6 is performed, resulting in the final value 7.
You can override the natural order of precedence by using parentheses. For instance, the expression (1 + 2) * 3 resolves to 9. The parentheses make the whole sub-expression 1 + 2 the left operand of the multiply (*) operator. Essentially, the sub-expression 1 + 2 is evaluated first, and the result is then used in the expression 3 * 3.
Operators are also said to have associativity, which is a way of describing which sub-expressions are performed first when the operators have equal precedence. In Setup Factory, most operators are left associative, which means that whenever two operators have the same precedence, the operation on the left is performed first. (The only exceptions are the unary plus, the unary minus, and the logical NOT operators, all three of which are right-associative.)
The left-associativity of the subtract (-) operator is why the expression 10 - 5 - 2 resolves to 3 instead of 7. It's interpreted as (10 - 5) - 2, and not 10 - (5 - 2).
SEE ALSO |
|
|
|
? |
For details on the usage and syntax of expressions, please see Chapter 15: Expressions in the User's Guide. |
See Also: Expressions, Values, How Values Are Stored, Operator Precedence and Associativity