Lab 3: Experiment 6


Operator Precedence

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 / 4 - 1
Should we go left to right:
((16 + 8) / 4) - 1 = (24 / 4) - 1 = 6 - 1 = 5
Should we go right to left:
16 + (8 / (4 - 1)) = 16 + (8 / 3) = 16 + 2 = 18
Should we start in the middle:
16 + (8 / 4) - 1 = 16 + 2 - 1 = 17
We get different answers depending on the order.

Modify your program to print the result of this expression without any parentheses. Compile and execute the program.

Question #3.6.1: Which of the orders listed above appears to be correct?

The choice is not arbitrary, but may seem a bit wierd. 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 precedence are applied before those with lower precedence. In C++, the precedence of the operators we have seen thus far are as follows:

Higher: () (parentheses)
! (logical NOT)
* (multiplication), / (division), % (modulus)
+ (addition), - (subtraction)
<< (ouput operator), >> (input operator)
< (less-than), <= (less-than-or-equal-to), > (greater-than) >= (greater-than-or-equal-to)
== (equality), != (inequality)
&& (logical AND),
Lower: || (logical OR),

So, from this list, we can see that / should be applied before + or -. That's why we should start in the "middle" of 16+8/4-1; we're really starting with /. It's irrelevant that it's in the middle. Wherever the division appeared (with these operators), we'd always do it first.

As indicated at the top of the list, 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.

Compute, by hand, the value of the following expression:

1 / 2 * (3 - 4) * 5 + (6 % 7 - (8 - 9) / 8)
Question #3.6.2: What is the result?

Then modify your source program to check your answer.

Question #3.6.3: What result did your program print?

Precedence and Boolean Expressions

Operator precedence is the reason why it is necessary to parenthesize a relational expression within an output statement. Consider this: write:

cout << i == j << endl;
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 (essentially) cout and j:
((cout << i) == j) << endl;
Since an ostream and an integer cannot be compared, the compiler generates an error for these implicit parentheses.

Question #3.6.4: What is the exact error message when you try to compile the parenthesis-free output statement?

If we instead parenthesize the expression explicitly like so:

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.

Terminology

operator precedence
Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.