4.10. Increment and Decrement Operators

In addition to the arithmetic assignment operators, C++ also provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig. 4.14. A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c = c + 1 or c += 1. An increment or decrement operator that’s prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively. An increment or decrement operator that’s postfixed to (placed after) a variable is referred to as the postfix increment or postfix decrement operator, respectively.

Image

Fig. 4.14. Increment and decrement operators.

Using the prefix increment (or decrement) operator to add (or subtract) 1 from a variable is known as preincrementing (or predecrementing) the variable. Preincrementing (or predecrementing) causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears. Using the postfix increment (or decrement) operator to add (or subtract) 1 from a variable is known as postincrementing (or postdecrementing) the variable. Postincrementing (or postdecrementing) causes the current value of the variable to be used in the expression in which it appears, then the variable’s value is incremented (decremented) by 1.

Figure 4.15 demonstrates the difference between the prefix increment and postfix increment versions of the ++ increment operator. The decrement operator (--) works similarly.


 1   // Fig. 4.15: fig04_15.cpp
 2   // Preincrementing and postincrementing.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      // demonstrate postincrement
 9      int c = 5; // assign 5 to c
10      cout << c << endl; // print 5
11      cout << c++ << endl; // print 5 then postincrement
12      cout << c << endl; // print 6                     
13
14      cout << endl; // skip a line
15
16      // demonstrate preincrement
17      c = 5; // assign 5 to c
18      cout << c << endl; // print 5                    
19      cout << ++c << endl; // preincrement then print 6
20      cout << c << endl; // print 6
21   } // end main


5
5
6

5
6
6


Fig. 4.15. Preincrementing and postincrementing.

Line 9 initializes c to 5, and line 10 outputs c’s initial value. Line 11 outputs the value of the expression c++. This postincrements the variable c, so c’s original value (5) is output, then c’s value is incremented. Thus, line 11 outputs c’s initial value (5) again. Line 12 outputs c’s new value (6) to prove that the variable’s value was incremented in line 11.

Line 17 resets c’s value to 5, and line 18 outputs that value. Line 19 outputs the value of the expression ++c. This expression preincrements c, so its value is incremented, then the new value (6) is output. Line 20 outputs c’s value again to show that the value of c is still 6 after line 19 executes.

The arithmetic assignment operators and the increment and decrement operators can be used to simplify program statements. The three assignment statements in Fig. 4.12

passes = passes + 1;
failures = failures + 1;
studentCounter = studentCounter + 1;

can be written more concisely with assignment operators as

passes += 1;
failures += 1;
studentCounter += 1;

with prefix increment operators as

++passes;
++failures;
++studentCounter;

or with postfix increment operators as

passes++;
failures++;
studentCounter++;

When you increment (++) or decrement (--) an integer variable in a statement by itself, the preincrement and postincrement forms have the same logical effect, and the predecrement and postdecrement forms have the same logical effect. It’s only when a variable appears in the context of a larger expression that preincrementing the variable and postincrementing the variable have different effects (and similarly for predecrementing and post-decrementing).


Image Common Programming Error 4.3

Attempting to use the increment or decrement operator on an expression other than a modifiable variable name, e.g., writing ++(x + 1), is a syntax error.


Figure 4.16 shows the precedence and associativity of the operators introduced to this point. The operators are shown top-to-bottom in decreasing order of precedence. The second column indicates the associativity of the operators at each level of precedence. Notice that the conditional operator (?:), the unary operators preincrement (++), predecrement (--), plus (+) and minus (-), and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. All other operators in Fig. 4.16 associate from left to right. The third column names the various groups of operators.

Image

Fig. 4.16. Operator precedence for the operators encountered so far in the text.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.103.10