Increment and Decrement Operators

Increment and Decrement Operators
Increment operator is written as a pair of plus signs i.e. ++ and decrement operator is written as a pair of minus signs i.e. --. Both are unary operators. That is they work on a single operand. The ++ operator increases the value of its operand by 1. On the other hand, the -- operator decreases the value of its operand by 1. Both operators exist in prefix form and postfix form. In prefix form they precede the operand and in postfix form they follow the operand. They are given here –

                                   Prefix Form of ++ and --    Postfix Form of ++ and –                                                                          
                                                ++ operand operand ++
                                                 -- operand   operand --


Consider a declaration –
int x = 10;
Here x has the value 10.  To increase the value of x by 1, generally you would use a statement –
x  = x + 1;
or its shorthand equivalent –
x += 1;
Using any of these two statements, you could increase the value of x by 1. That is, after any of the two statements, x will be 11.
There is one more way to increase the value of x by 1 – use increment operator i.e. ++.
Suppose x = 10
Now after this statement –
++x;
The value of x will be 11. That is the ++ has increased the value of x by 1. This is equivalent to the statement x = x + 1. 
After another statement –
x++;
The value of x will be 12. That is, the ++ again, has increased the value of x by 1. This too, is equivalent to write x = x + 1. As you can see, ++x and x++ work similarly and both are equivalent to write x = x + 1. In ++x the prefix form of ++ is used and in x++ the postfix form of ++ is used.

The -- operator decreases the value of its operand by one. If you use --x or x--, it will decrease the value of x by 1. This is equivalent to write x = x – 1.

As you saw, the prefix and postfix form of both ++ and -- work same when they are used independently, that is without combining in expressions. But, the prefix and postfix form of both ++ and -- work differently when they are used in expressions. The prefix form modifies the value of the variable first, and then the modified value is used in expression. On the other hand, the postfix form modifies the value of the variable after the expression is evaluated using the original value. Here is an example how these two forms works.

Suppose, x = 10
Then the statement -
y = ++x;
will increases the value of x first, and then it is assigned to y. That is x is incremented to 11 first, and then y gets the incremented value 11 from x. This is equivalent to write –
x = x + 1;       
y = x;
On the other hand, in the statement –
y = x++;
The value of x i.e. 10 is first assigned to y, and then x is incremented to 11. This is equivalent to write –
y = x;
x = x + 1;
As you saw, in both the cases x is incremented to 11. But, in first case y gets the incremented / modified value of x i.e. 11 whereas in second y gets the original value of x i.e. 10. The same difference will occur between y = --x and y = x --. In first case, y = --x, the value of x will be decremented first and then assigned to y. That is, y will get the decremented value of x not the original one. In second case, y = x--, the value of x will be assigned to y first and then decremented. That is, y will get the original value of x not the decremented one.   

Using increment and decrement operators to increase and decrease the value of variable by 1 is much better than simple / shorthand assignment operators. To increase / decrease the value by another degree, use shorthand assignment operators.   


NEXT   :sizeof operator
PREVIOUS :  Assignment Operators 

1 comment: