C++ features several operators that are frequently used in loops; let’s take a little time to examine them now. You’ve already seen two: the increment operator (++), which inspired the name C++, and the decrement operator (--). These operators perform two exceedingly common loop operations: increasing and decreasing a loop counter by one. However, there’s more to their story than you’ve seen to this point. Each operator comes in two varieties. The
Listing 5.7. plus_one.cpp
// plus_one.cpp -- the increment operator
#include
int main()
{
using std::cout;
int a = 20;
int b = 20;
cout << "a = " << a << ": b = " << b << "\n";
cout << "a++ = " << a++ << ": ++b = " << ++b << "\n";
cout << "a = " << a << ": b = " << b << "\n";
return 0;
}
Here is the output from the program in Listing 5.7:
a = 20: b = 20
a++ = 20: ++b = 21
a = 21: b = 21
Roughly speaking, the notation a++ means “use the current value of a in evaluating an expression, and then increment the value of a.” Similarly, the notation ++b means “first increment the value of b and then use the new value in evaluating the expression.” For example, we have the following relationships:
int x = 5;
int y = ++x; // change x, then assign to y
// y is 6, x is 6
int z = 5;
int y = z++; // assign to y, then change z
// y is 5, z is 6
Using the increment and decrement operators is a concise, convenient way to handle the common task of increasing or decreasing values by one.
The increment and decrement operators are nifty little operators, but don’t get carried away and increment or decrement the same value more than once in the same statement. The problem is that the use-then-change and change-then-use rules can become ambiguous. That is, a statement such as the following can produce quite different results on different systems:
x = 2 * x++ * (3 - ++x); // don't do it except as an experiment
C++ does not define correct behavior for this sort of statement.
Side Effects and Sequence Points
Let’s take a closer look at what C++ does and doesn’t say about when increment operators take effect. First, recall that a
What’s a full expression? It’s an expression that’s not a subexpression of a larger expression. Examples of full expressions include an expression portion of an expression statement and an expression that serves as a test condition for a while loop.
Sequence points help clarify when postfix incrementation takes place. Consider, for instance, the following code:
while (guests++ < 10)
cout << guests << endl;
(The while loop, discussed later this chapter, works like a for loop that has just a test expression.) Sometimes C++ newcomers assume that “use the value, then increment it” means, in this context, to increment guests after it’s used in the cout statement. However, the guests++ < 10 expression is a full expression because it is a while loop test condition, so the end of this expression is a sequence point. Therefore, C++ guarantees that the side effect (incrementing guests) takes place before the program moves on to cout. Using the postfix form, however, guarantees that guests will be incremented after the comparison to 10 is made.
Now consider this statement:
y = (4 + x++) + (6 + x++);
The expression 4 + x++ is not a full expression, so C++ does not guarantee that x will be incremented immediately after the subexpression 4 + x++ is evaluated. Here the full expression is the entire assignment statement, and the semicolon marks the sequence point, so all that C++ guarantees is that x will have been incremented twice by the time the program moves to the following statement. C++ does not specify whether x is incremented after each subexpression is evaluated or only after all the expressions have been evaluated, which is why you should avoid statements of this kind.