The increment and decrement operations In C++

label Computer Science
account_circle Unassigned
schedule 1 Day
account_balance_wallet $5

  • How increment and decrement operations work with combined assignments (also, look out for cases of integer division) 
Nov 19th, 2017

The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand. Thus:

x = x+1;is the same as

And similarly:

x = x-1;is the same as

Both the increment and decrement operators can either precede (prefix) or follow (postfix) the operand. For example:

x = x+1;
can be written as++x;// prefix form

or as:

x++;// postfix form

When an increment or decrement is used as part of an expression, there is an important difference in prefix and postfix forms. If you are using prefix form then increment or decrement will be done before rest of the expression, and if you are using postfix form, then increment or decrement will be done after the complete expression is evaluated.


Following is the example to understand this difference:

#include<iostream>usingnamespace std;
main(){int a =21;int c ;// Value of a will not be increased before assignment.
   c = a++;   
   cout <<"Line 1 - Value of a++ is :"<< c << endl ;// After expression value of a is increased
   cout <<"Line 2 - Value of a is :"<< a << endl ;// Value of a will be increased before assignment.
   c =++a;  
   cout <<"Line 3 - Value of ++a is  :"<< c << endl ;return0;}

When the above code is compiled and executed, it produces the following result:

Line1-Value of a++is:21Line2-Value of a is:22Line3-Value of ++a is:23

Nov 19th, 2014

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
Nov 19th, 2017
Nov 19th, 2017
Nov 19th, 2017
Mark as Final Answer
Unmark as Final Answer
Final Answer

Secure Information

Content will be erased after question is completed.

Final Answer