Increment Decrement Operators

C++ includes two useful operators. These are increment and decrement operators, ++ and –.

  1. Increment Operator (++) – add 1 to its operand.
    • Syntax :    ++variable_name; (or) variable_name++;
    • Versions:
      • Preincrement (++variable_name) : Value of variable is incremented before assigning it to variable.
        Example : ++m;
      • Postincrement (variable_name++) : Value of variable is incremented after assigning it to variable.
        Example : –m;
  2. Decrement Operators (–) → subtracts 1 from the operand
    • Syntax :   —variable_name; (or) variable_name–;
    • Versions:
      • Predecrement (–variable_name) : Value of variable is incremented before assigning it to variable.
        Example : ++m;
      • Postdecrement (variable_name–) : Value of variable is incremented after assigning it to variable.
        Example : –m;

Increment Decrement Operators :

 S.no Operator type Operator Description
1 Pre-increment ++p Value of p is incremented before
assigning it to variable p.
2 Post-increment p++ Value of p is incremented after
assigning it to variable p.
3 Pre-decrement – –p Value of p is decremented before
assigning it to variable p.
4 Post-decrement p– – Value of p is decremented after
assigning it to variable p.

Difference between preincrement and postincrement operators in C++ with an example.

#include<iostream.h>
int main()
   {
   clrscr();
   int a=6,x;
   x=++a + 2*a;
   cout<<"when a=6, calculate x=\n";
   cout<<"\n1)x=(++a + 2*a)\n x="<<x;
   a=6;
   x=a++ + ++a;
   cout<<"\n2)x=(a++ + ++a)\n x="<<x;
   a=6;
   x=++a + a++;
   cout<<"\n3)x=(++a + a++)\n x="<<x;
   a=6;
   x=++a + ++a;
   cout<<"\n4)x=(++a + ++a) \n x="<<x;
   a=6;
   x=a++ + a++;
   cout<<"\n5)x=(a++ + a++) \n x="<<x;
   return 0;
   }

Output :

when a=6, calculate x=
1)x=(++a + 2*a)
  x=21
2)x=(a++ + ++a)
  x=14
3)x=(++a + a++)
  x=14
4)x=(++a + ++a) 
  x=16
5)x=(a++ + a++)
  x=12
     

Incoming search terms:

  • increment and decrement operators in c with examples pdf

This article has 5 comments

  1. Pingback: Nike Free Run 2

  2. Pingback: cheap air max 95 for sale

  3. Pingback: cheap where to buy nike air max 2013

  4. Pingback: ray ban pris usa

  5. Pingback: nike air max 2 size uk

Leave a Reply