Operator Evaluation Order
The operator precedence and associativity rules of a language dictate the order of evaluation of its operators.
The value of an expression depends at least in part on the order of evaluation of the operators in the expression. Consider the following expression:
a b * c
Suppose the variables a, b, and c have the values 3, 4, and 5, respectively. If evaluated left to right (the addition first and then the multiplication), the result is 35. If evaluated right to left, the result is 23.
Instead of simply evaluating the operators in an expression from left to right or right to left, mathematicians long ago developed the concept of placing operators in a hierarchy of evaluation priorities and basing the evaluation order of expressions partly on this hierarchy. For example, in mathematics, multiplication is considered to be of higher priority than addition, perhaps due to its higher level of complexity. If that convention were applied in the previous example expression, as would be the case in most programming languages, the multiplication would be done first.
The operator precedence rules for expression evaluation partially define the order in which the operators of different precedence levels are evaluated. The operator precedence rules for expressions are based on the hierarchy of operator priorities, as seen by the language designer. The operator precedence rules of the common imperative languages are nearly all the same, because they are based on those of mathematics. In these languages, exponentiation has the highest precedence (when it is provided by the language), followed by multiplication and division on the same level, followed by binary addition and subtraction on the same level.
Many languages also include unary versions of addition and subtraction. Unary addition is called the identity operator because it usually has no associated
operation and thus has no effect on its operand. Ellis and Stroustrup (1990, p. 56), speaking about C , call it a historical accident and correctly label it useless. Unary minus, of course, changes the sign of its operand. In Java and C#, unary minus also causes the implicit conversion of short and byte operands to int type.
In all of the common imperative languages, the unary minus operator can appear in an expression either at the beginning or anywhere inside the expression, as long as it is parenthesized to prevent it from being next to another operator.
a (- b) * c
is legal, but
usually is not.
Next, consider the following expressions:
- a / b
- a * b
- a ** b
In the first two cases, the relative precedence of the unary minus operator and the binary operator is irrelevant—the order of evaluation of the two operators has
no effect on the value of the expression. In the last case, however, it does matter. Of the common programming languages, only Fortran, Ruby, Visual Basic, and Ada have the exponentiation operator. In all four, exponentiation has higher precedence than unary minus, so
- A ** B
is equivalent to
-(A ** B)
The precedences of the arithmetic operators of Ruby and the C-based languages are as follows:
The ** operator is exponentiation. The % operator takes two integer operands and yields the remainder of the first after division by the second.