Tuesday, 18 October 2011



Increment(++) and Decrement Operators(--)


C,Java and many other languages provide operators:
these two operators are also belong to them 

1. auto increment operators.
2. auto decrement operators.

 In their simplest forms, they look like this:


++i add 1 to i

--j subtract 1 from j


These correspond to the slightly longer i += 1 and j -= 1, respectively, and also to the fully ``longhand'' forms i = i + 1 and j = j - 1.




The ++ and -- operators apply to one operand (they're unary operators).
The expression ++i adds 1 to i, and stores the incremented result back in i. 
This means that these operators don't just compute new values; they also modify the value of some variable.

These operators are unique in that they can appear both in postfix form, where they follow the operand as just shown:

                       i++
                                 i--

and prefix form, where they precede the operand:

                      --i
                      ++i


Example with explanation:
When the increment and decrement operators are part of a larger expression, then a subtle, yet powerful, difference between these two forms appears.

In the prefix form, the operand is incremented or decremented before the value is obtained for use in the expression:

                                  (Modify then change)

For example:

              x = 10;
              y = ++x;

In this case, y is set to 11 as you would expect, because the increment occurs before x is assigned to y.

thus, the line y=++x; is the equivalent of these two statements:

x=10;
y=x+1;
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------

In postfix form, the previous value is obtained for use in the expression, and then the operand is modified:
                         
                                  (Change then Modify)

For example:
            x = 10;
            y = x++;

The value of x is obtained before the increment operator is executed, so the value of y is 10.


Here, the line y = x++; is the equivalent of these two statements:

             y =x;
            x = x + 1;


Note: Of course, in both cases x is set to 11.

Example:

//Prefix increment decrement  

#include<Stdio.h>
#include<conio.h>
void main()
{
         int i,d,inc,dec;
         inc=10;
         dec=20;
    
        printf("Initial value:\nInc=%d\n Dec=%d",inc,dec);      
          //prefix operation:
         
         i=++inc;
         d=--dec;               

       printf("\n\nAfter Prefix operation value:\n i=%d\n d=%d",i,d);      


       printf("\n\n Prefix operation value:\nInc=%d\n Dec=%d",inc,dec);      

         getch();
}


Result :
             Initial value:
             Inc=10
             Dec=20

            After Prefix operation value:
            i=11
            d=19

          Prefix operation value:
            Inc=11
            Dec=19

--------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Postfix increment decrement  

#include<Stdio.h>
#include<conio.h>
void main()
{
         int i,d,inc,dec;
         inc=10;
         dec=20;
    
        printf("Initial value:\n inc=%d\n dec=%d",inc,dec);      
          
      //postfix operation:
         
         i=inc++;
         d=dec--;               
       
       printf("After Postfix operation value:\n i=%d\n d=%d",i,d);      

      printf(" \n\nPostfix operation value:\n inc=%d\n dec=%d",inc,dec);      

         getch();
}


Result :
             Initial value:
             inc=10
             dec=20

            After Prefix operation value:
            
           i=10
           d=20

         Postfix operation value:
         inc=11
        dec=19

No comments:

Post a Comment