Postfix and Prefix modes of the increment and decrement operators

posted Feb 11, 2013, 12:57 AM by Karwan Jacksi   [ updated Feb 11, 2013, 12:58 AM ]
// This program demonstrates the postfix and prefix
// modes of the increment and decrement operators.

#include <iostream>
using namespace std;

int main()
{
int num = 4;

// Illustrate postfix and prefix ++ operator
cout << num << " "; // Displays 4
cout << num++ << " "; // Displays 4, then adds 1 to num
cout << num << " "; // Displays 5
cout << ++num << "\n\n"; // Adds 1 to num, then displays 6

// Illustrate postfix and prefix -- operator
cout << num << " "; // Displays 6
cout << num-- << " "; // Displays 6, then subtracts 1 from num
cout << num << " "; // Displays 5
cout << --num << "\n\n"; // Subtracts 1 from num, then displays 4

return 0;
}

Program Output
4 4 5 6
6 6 5 4
Comments