The ++ and -- operators.

Post date: Feb 11, 2013 8:53:49 AM

// This program demonstrates the ++ and -- operators.

#include <iostream>

using namespace std;

int main()

{

int num = 4; // num starts out with 4

// Display the value in num

cout << "The variable num is " << num << endl;

cout << "I will now increment num.\n\n";

// Use postfix ++ to increment num

num++;

cout << "Now the variable num is " << num << endl;

cout << "I will increment num again.\n\n";

// Use prefix ++ to increment num

++num;

cout << "Now the variable num is " << num << endl;

cout << "I will now decrement num.\n\n";

// Use postfix -- to decrement num

num--;

cout << "Now the variable num is " << num << endl;

cout << "I will decrement num again.\n\n";

// Use prefix -- to increment num

--num;

cout << "Now the variable num is " << num << endl;

return 0;

}

Program Output

The variable num is 4

I will now increment num.

Now the variable num is 5

I will increment num again.

Now the variable num is 6

I will now decrement num.

Now the variable num is 5

I will decrement num again.

Now the variable num is 4