1: //increment_decrement.cpp
2: //Illustrates why you shouldn't use ++ or --, except in a
3: //standalone statement that increments or decrements a variable.
5: #include <iostream>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program shows you some examples of situations in "
11: "which the increment\nand decrement operators, ++ and --, are "
12: "sometimes used, but shouldn't be,\nbecause the code is not "
13: "very readable, and is fraught with other dangers.\nStudy the "
14: "code, predict the output in each case, and then ...\n\n";
16: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
17: cout << endl << endl;
19: int i, j;
21: i = 4;
22: j = i++ + 1;
23: cout << i << " " << j << endl;
24: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
26: i = 4;
27: j = ++i + 1;
28: cout << i << " " << j << endl;
29: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
31: i = 4;
32: j = i-- + 1;
33: cout << i << " " << j << endl;
34: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
36: i = 4;
37: j = --i + 1;
38: cout << i << " " << j << endl;
39: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
41: cout << endl;
43: i = 25;
44: cout << i << endl;
45: cout << ++i << endl;
46: cout << i << endl;
47: cout << i++ << endl;
48: cout << i << endl;
49: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
50: }