Successfully reported this slideshow.
Upcoming SlideShare
×

# Lecture 8 increment_and_decrement_operators

3,127 views

Published on

Programming C increment and decrement

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Lecture 8 increment_and_decrement_operators

1. 1. Increment and Decrement Operators www.eshikshak.co.in
2. 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 is old value and multiply it by 2, increment i, store the result of the multiplication in k ) www.eshikshak.co.in
3. 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 is old value and multiply it by 2, decrement i, store the result of the multiplication in k ) www.eshikshak.co.in
4. 4. 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
5. 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. 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. 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. 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. 9. Precedence of Increment andDecrement 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. 10. Precedence of Increment andDecrement 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. 11. www.eshikshak.co.in