Diference between pre Increment and post increment (i++ vs ++i)
The increment operator increases its operand by one.The decrement operator decreases its operand by one.for example this statement x=x+1 can be rewritten like this by use of increment operator x++,
similarly the statement x=x-1 is equivalent to x–.
Is thre any diference in Pre-increment ++i and Post-increment i++ ??
In the forgoing example thre is no diference in prefix and postfix forms.
However when the increment and decrement operator are part of a large expression then a subtle,yet powerful diference b/w these two forms apears.
In the Prefix form ,the operand is incremented or decremented before the value is obtained for use in the expression.
In Postfix form the previous value is obtatined for use in the expression ,and then the operand is modified.
in this case y is set to 66 as you would expect,because the increment occurs before x is asigned to y. thus the line ++x ; is equivalent of these two statements;
Howewer,when written like this:
the value x is obtained before the increment operator is executed ,so the value of y is 65. Of course in both cases x is set to 66 .Here the line y=x++; is equivalent to these two statements:
Latest posts by Ravindra Solanki (see all)
- An Introduction to Apache Kafka - April 5, 2018
- How To Hide Secret Text Behind Image Using Steganography - May 17, 2016
- Founder OF Hackers Den ,Quits! - April 1, 2016