Increment & Decrement Operators In C++


Increment and decrement operators are important to understand, but most importantly to get right!  These operators work just as they sound:

increment = to increase by one  & decrement = decrease by one.

The increment operator is ++ and the decrement operator is .

Understanding these two operators is not so difficult.  However, depending how you write them determines how they will be implemented in a program.  In other words, by “changing them around, you get different results” (Huber, J).

“In simple statements, it doesn’t matter if the increment or decrement operator is used”. ( Gaddis, pg. 251)  It depends on which mode is being implemented.  There are two different modes to consider here, Postfix Mode and Prefix Mode.  “Postfix mode causes the increment/decrement to happen after the value of the variable is used in the expression”! (Gaddis, pg. 251)  Prefix mode causes the increment/decrement to happen before the value of the variable is used in the expression!

Here is an example demonstrating Postfix and Prefix Modes:

int main( )

{

int num = 8; //variable value

cout << num << endl; //displays the number 8

cout << num++ << endl; // Postfix – displays the number 8 & then adds 8 + 1

cout << num << endl; // displays the number 9

cout << endl;

cout << num << endl; //displays the number 9

cout << ++num << endl; // Prefix – adds 9 + 1 & then displays the number 10

cout << endl;

cout << num << endl; //displays the number 10

cout << num– << endl; // Postfix – displays the number 10 & then subtracts 10 – 1

cout << num << endl; // displays the number 9

cout << endl;

cout << num << endl; //displays the number 9

cout << –num << endl; // Prefix – subtracts 9 – 1 & then displays the number 8

cout << endl;

return 0;

}

Here is the Program Output:

8

8

9

9

10

10

10

9

9

8

As you can see from the above Program example, where you place the increment/decrement operator is very important to insure you get your desired output.  This is where the “uniqueness” comes into play!

Below is a Simple Loop Program that demonstrates an output that counts up from 1 to 100!

#include <iostream>

using namespace std;

int main()

{

  int count;

  for(count=1; count <= 100; count=count+1) 

    cout << count << ” “ ;

  system (“pause“);

  return 0;

}

Below is a Simple Loop Program that demonstrates an output that counts down from 100 to 1!

#include <iostream>

using namespace std;

int main()

{

  int count;

  for(count=100; count >= 1; count=count-1) 

    cout << count << ” ” ;

  system (“pause“);

  return 0;

}

Reference:  Gaddis, Tony. 2009. Starting Out With C++, From Control Structures through Objects, 6th Edition. Pearson Education, Inc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s