Precedence is the set of rules imposed upon the evaluation of components of an expression that are not otherwise overridden by the presence of parentheses.
Based on the operation, expressions are evaluated from left-to-right (left-associative), from right-to-left (right-associative), or with no association (nonassociative). For example, multiplication and division are left-associative. The arithmetic expression below evaluates left-to-right:
x = 9 / 3 * 2
This first divides 9 by 3, yielding 3, which is then multiplied by 2, yielding the result 6. This expression evaluated as a right association returns the result 1.5.
Logical operations are nonassociative. For example, the expression below does not compile:
x = 1 < x < 3
Adding parentheses makes it a valid expression:
x = (1 < x) < 3
Adding the parentheses, however, does not return the results of (1 < x) and (x < 3).
x = (99 < 1) < 3 x = (0 < 1) < 3
The above two examples both evaluate as 1. In the first example, (99 < 1) evaluates as 0, and 0 < 3 evaluates as 1. In the second example, (0 < 1) evaluates as 1 and 1 < 3 evaluates as 1.
Below is a list of operations, precedence and associativity:
|<>||1||Dynamic array reference||None|
|eq, =||7||Logical equal||None|
|ne, #||7||Not equal||None|
|lt, <||7||Less than||None|
|le, <=||7||Less than or =||None|
|gt, >||7||Greater than||None|
|ge, >=||7||Greater than or =||None|
|and, &||8||Logical and||Left|
|or, !||8||Logical or||Left|
Expressions are evaluated in order of precedence unless placed within parentheses. 10+2*10 is evaluated as 30. Expressions within the innermost parentheses are evaluated first. (10+2)*10 evaluates as 120.
() reserved characters, * arithmetic operator, *= assignment operator, + arithmetic operator, += assignment operator, - arithmetic operator, -= assignment operator, /= assignment operator, := assignment operator, Arithmetic expressions, Arithmetic operators, Boolean evaluation, Logical expressions, Relational operators, \= assignment operator