Contents
- Index
Precedence and Association
The operator precedence and association rules determine the default order that the operators in expressions are performed. This order can always be overridden using parenthesis.
The operations are first performed in the order of precedence. The highest precedence operators are performed first. The precedence of the different operators are given below starting with those with the highest precedence.
(1) - (negative)
(2) ^
(3) *, /
(4) + , - (subtraction)
Operators that have the same precedence are performed according to their order. Sometimes they are performed from left to right and sometimes from right to left. The order they are performed in is called their association. The associations of the different operators are given below:
Left to Right: *, /, +, - (subtraction)
Right to Left: - (negative), ^
If in doubt about the order the operations will be performed, use parenthesis.
Examples:
1 + 2 * 3 = 1 + (2 * 3) = 7
-1 ^ 2 = (-1) ^ 2 = 1
8 / 2 * 3 = (8 / 2) * 3 = 12
2 ^ 1 ^ 2 = 2 ^ (1 ^ 2) = 2