Increment and Decrement Operators

                   www.eshikshak.co.in
Increment Operator (++)

 ● The increment operator (++) adds 1 to its operand.
 ● It is an unary operator
  ( A operator that requires only one operand is known as
  unary operator )
 ● Auto-increment
 ● i++ add 1 to i
 ● i = i + 1 can be represented in two forms as
   1) i += 1 2) i++
 ● k = 2 * i++
  ( means ``take i's old value and multiply it by 2, increment i,
  store the result of the multiplication in k'' )



                     www.eshikshak.co.in
Decrement Operator (--)

 ● The decrement operator (++) subtract 1 from its
   operand.
 ● It is an unary operator
   ( A operator that requires only one operand is known as
   unary operator )
 ● Auto-decrement
 ● i-- subtracts 1 from i
 ● i = i - 1 can be represented in two forms as
   1) I -= 1 2) i--
 ● k = 2 * i--
   ( means ``take i's old value and multiply it by 2, decrement i,
   store the result of the multiplication in k'' )


                     www.eshikshak.co.in
Postfix and Prefix Forms

 ● There are two forms for each of the operators:
   postfix and prefix
 ● The statement ++i (prefix form) increments i
   before using its value
 ● The statement i++ (postfix form) increments it
   after its value has been used.
 ● The postfix increment and decrement
   operators fetch the current value of the
   variable
 ● Store a copy of it in a temporary location. The
   compiler then increments or decrements the
   variable
                www.eshikshak.co.in
Postfix Forms - Example

 ● i = 5;
   j = i++;
   printf(“nj : %d and i : %d”, j, i);
 ● Output :

   j : 5 and I : 6




                     www.eshikshak.co.in
Postfix Forms - Example

 ● i = 5;
   j = i++;
   printf(“nj : %d and i : %d”, j, i);
   printf(“ni : %d”,i++);
   printf(“ni : %d”,i);
 ● Output :

   j : 5 and i : 6
   i:6
   i:7




                     www.eshikshak.co.in
Prefix Forms - Example

 ● i = 5;
   j = ++i;
   printf(“nj : %d and i : %d”, j, i);
 ● Output :

   j : 6 and i : 6




                     www.eshikshak.co.in
Prefix Forms - Example

 ● i = 5;
   j = ++i;
   printf(“nj : %d and i : %d”, j, i);
   printf(“ni : %d”,++i);
   printf(“ni : %d”,i);
 ● Output :

   j : 6 and i : 6
   i:7
   i:7




                     www.eshikshak.co.in
Precedence of Increment and
Decrement Operators
 ● The increment and decrement operators have
   the same precedence, but bind from right to
   left. So the expression
 ● --j++
s evaluated as
--(j++)




                 www.eshikshak.co.in
Precedence of Increment and
Decrement Operators -
Examples the value of k in i then
 ● i=k--; /* Stores
  decrements k. */
 ● J=i++; /* Stores the value of l in j then
   increments i. */
 ● i=--k; /* Decrements k then stores the new
   value of k in i. */
 ● j=++l; /* Increments l then stores the new value
   of l in j. */




                 www.eshikshak.co.in
www.eshikshak.co.in

Lecture 8 increment_and_decrement_operators

  • 1.
    Increment and DecrementOperators www.eshikshak.co.in
  • 2.
    Increment Operator (++) ● The increment operator (++) adds 1 to its operand. ● It is an unary operator ( A operator that requires only one operand is known as unary operator ) ● Auto-increment ● i++ add 1 to i ● i = i + 1 can be represented in two forms as 1) i += 1 2) i++ ● k = 2 * i++ ( means ``take i's old value and multiply it by 2, increment i, store the result of the multiplication in k'' ) www.eshikshak.co.in
  • 3.
    Decrement Operator (--) ● The decrement operator (++) subtract 1 from its operand. ● It is an unary operator ( A operator that requires only one operand is known as unary operator ) ● Auto-decrement ● i-- subtracts 1 from i ● i = i - 1 can be represented in two forms as 1) I -= 1 2) i-- ● k = 2 * i-- ( means ``take i's old value and multiply it by 2, decrement i, store the result of the multiplication in k'' ) www.eshikshak.co.in
  • 4.
    Postfix and PrefixForms ● There are two forms for each of the operators: postfix and prefix ● The statement ++i (prefix form) increments i before using its value ● The statement i++ (postfix form) increments it after its value has been used. ● The postfix increment and decrement operators fetch the current value of the variable ● Store a copy of it in a temporary location. The compiler then increments or decrements the variable www.eshikshak.co.in
  • 5.
    Postfix Forms -Example ● i = 5; j = i++; printf(“nj : %d and i : %d”, j, i); ● Output : j : 5 and I : 6 www.eshikshak.co.in
  • 6.
    Postfix Forms -Example ● i = 5; j = i++; printf(“nj : %d and i : %d”, j, i); printf(“ni : %d”,i++); printf(“ni : %d”,i); ● Output : j : 5 and i : 6 i:6 i:7 www.eshikshak.co.in
  • 7.
    Prefix Forms -Example ● i = 5; j = ++i; printf(“nj : %d and i : %d”, j, i); ● Output : j : 6 and i : 6 www.eshikshak.co.in
  • 8.
    Prefix Forms -Example ● i = 5; j = ++i; printf(“nj : %d and i : %d”, j, i); printf(“ni : %d”,++i); printf(“ni : %d”,i); ● Output : j : 6 and i : 6 i:7 i:7 www.eshikshak.co.in
  • 9.
    Precedence of Incrementand Decrement Operators ● The increment and decrement operators have the same precedence, but bind from right to left. So the expression ● --j++ s evaluated as --(j++) www.eshikshak.co.in
  • 10.
    Precedence of Incrementand Decrement Operators - Examples the value of k in i then ● i=k--; /* Stores decrements k. */ ● J=i++; /* Stores the value of l in j then increments i. */ ● i=--k; /* Decrements k then stores the new value of k in i. */ ● j=++l; /* Increments l then stores the new value of l in j. */ www.eshikshak.co.in
  • 11.