Programming with Passion

Make the best out of everything.

Monday, 12 January 2015

INCREMENT OR DECREMENT (Difference in postfix and prefix)

Well some of you might be having little difficulties on increment or decrement with postfix and prefix.
So I'm gonna tell you that with examples.

First of all we'll start with postfix increment/decrement .
In postfix increment the value is incremented after its first use. Like
e.g.
A=5, B;
B=A++;
here B will be assigned the value of A which is 5. after that a will be incremented i.e. A will have now value 6.
Another e.g. to understand well
C=12, D, E;
D=C++;
E=C;
Now have a go in this. Think of what the answer could be.
Got It.
Here is the solution. In this C is initialized a value 12 while D and E are just initialized. In the second line D is assigned the value of C which is 12. and C is incremeted. i.e. now it will be 13. And on the third line E is assigned the value of C which is it will be 13 bcoz C is incremented.
For decrement it is the same but the value decreases not increases.
In short it delays the process of increament/decrement on that line.
Good luck with learning.
Happy learning.


No comments:

Post a Comment