Increment and Decrement operators

Increment operators (++)

  • Increment operators are increase the value of subsequent. value may be increase according to the programmer.
  • Increment operator are two types as follows :
    1. Post increment
    2. Pre increment
 Decrement operators ( -- )
  • decrement operators decrease the value to one, two and so on.
  • As like Increment operators, decrement operators are also two type as : 
    1. Post decrement
    2. Pre decrement
Before we more discuss about increment and decrement operator, let understand some expression; that's are using in all operators.

Now some issues of pre and post increment and decrement, we will discuss after solve following programming :

 x = 5       x = 5     x = 5  
 x = x+1      ++x       x++    
 x = 6       x = 6      x=5       

In above program u notice that value of x increase to one in all three expression but after the increasing one, the final value of x in not same, third box result is differ to another two boxes. Now you know that c operators are mostly three types, ++ and -- operators are Unary operators.

Post increment or decrement are lowest precedence so it is solve at the end of program. Hence above program in third box first value of x is print i.e. 5 after, it is increase.

Unary operators are solve right to left.


Q.1 Solve the expression in C language compiler ,assuming x,z are integer variables:
z = x++ + x++
where x=7
Ans.
stepwise evaluation of this expression is shown below :

 z = x++ + x++ + ++x
 //post increment of x=2
 z = x + x + ++x
 //++x increase the value of x one so x=8 
 /*Now all pre and post increment/decrement are  solved so we write the the expression in normal mathematical expression*/
 z = x + x + x
 //put the current value of x i.e. 8
 z = 8 + 8 8
 z = 24


syntax : x=current value-post increment/decrement
so x = 8 + 2
   x = 10

Q.2 Solve the following expression : 
z = ++x + y-- - ++y - x-- - x-- - ++y - x--
where   x = 7
        y = -3
Ans.
 z=++x + y-- - ++y - x-- - x-- - ++y - x--
 /* post decrement of x=3 and
    post decrement of y=1 */
 z= ++x + y - ++y - x - x - ++y - x
 /* its unary operator so we solve it from
 right to left*/
 z=++x + y - ++y - x - x - y - x
 //now y=-3+1=-2
 z=++x + y - y - x - x - y - x
 //now y=-2+1=-1
 z=x + y - y - x - x - y - x
 //now x=7+1=8
 /*now all increment and decrement operators  
 all solved so we put the current value of x  
 and y.*/
 z= 8 + (-1) - (-1) -- 8 - (-1) - 8
 z= 8 + 1 - 8 + 1 - 8
 z= - 16 + 1
 z= -15

 x=8-3=5  
 y=-1-1=-2


Related some increment or decrement operators exercise:

Q.3  solve the following equations:

z = x++ + ++y - x-- + --y

where x = 7 and 
         y = 9

hint:(For your cross-checking, answer is x=7, y=9 and z=18).

Q.4 solve the following equations:

z = x++ * y++ / ++x - --y  % x++

where x = 5 and
         y = 2

hint:(For your cross-checking, answer is x=8, y=2 and z=0).

--------------------------------------------------------------------------------

Q.3 answer with explanation :

z = x++ + ++y - x-- + --y


where x = 7 and 
         y = 9

Ans:

post decrement of x = 1
post increment of x = 1

z = x + ++y - + --y
z = x + ++y - x + y  // now y = 9-1 = 8
z = x + y - x + y      // now y = 8+1 = 9
// now put the value of x and y
z = 7 + 9 - 7 + 9
z = 9 + 9
z = 18

Hence, our final answer will be:

x = 7 - 1 + 1 = 7
y = 9
z = 18


Related program:

What will be output of below program:

int i=1;
printf("%d%d%d",i,++i,i++);

Click here for answer.

Comments