The arithmetic operations of addition, subtraction, multiplication and division are familiar ones. However, when these operations are mixed within an expression, some mechanism must be used to determine which operation is to be performed first. This is important because the value of the expression can change, based on the order in which operators are applied.
To illustrate, consider the expression:
16 + 8 / 4If the operations are simply performed from left-to-right (+, then /), the expression evaluates to
(16 + 8) / 4 = 24 / 4 = 6but if the division operation for some reason is treated as "more important" and performed before the addition operation, then the expression evaluates to
16 + (8 / 4) = 16 + 2 = 18The result of an expression thus depends on the order in which operators are applied. Using your source program, find out which operator is applied first in the preceding expression, and report it below, before continuing:
The order in which operators are applied is determined by a characteristic called operator precedence. Each C++ operator is given a precedence or priority level, and operators with higher priority levels are applied before those with lower priority levels. In C++, the precedence of the operators we have seen thus far are as follows:
| Higher: | () (function call or type construction), |
| + (positive), - (negative), ! (logical NOT) | |
| * (multiplication), / (division), % (modulus) | |
| + (addition), - (subtraction) | |
| << (ostream insertion), >> (istream extraction) | |
| < (less-than), <= (less-than-or-equal-to), > (greater-than) >= (greater-than-or-equal-to) | |
| == (equality), != (inequality) | |
| && (logical AND), | |
| Lower: | || (logical OR), |
As indicated, parentheses can be used to change the order in which operators are evaluated, with the operator in the inner-most parentheses being applied first. For example, in the expression
((9-1) * (1+1))the subtraction is performed first, then the addition, and finally the multiplication.
Using "pencil and paper," find the value of the following expression:
1 / 2 * (3 - 4) * 5 + (6 % 7 - (8 - 9) / 8)
Operator precedence is the reason why it is necessary to parenthesize a relational expression within an output statement. If we were to write:
cout << i == j << endl;Then the insertion operator has higher precedence than the equality operator, so the value of i would be inserted into cout, then the equality operator would be applied to cout and j. Since an ostream and an integer cannot be compared, the compiler generates an error.
Using express.cpp, type in this (unparenthsized) output statement and in the space below, record (i) whether the error occurs at compile-time or at run-time, and (ii) the error message:
If we instead parenthesize the expression:
cout << (i == j) << endl;then the equality operator is applied first, producing a boolean value, then the leftmost insertion operator is applied, inserting that boolean value into cout. The rightmost insertion operator is then applied, inserting endl into cout. We'll see why in the next experiment.
You should now understand what operator precedence is, and how C++ uses it to determine the order in which operators are applied.
Forward to the Next Experiment