Prefix Increment and Decrement Operator
In prefix form, the operand is incremented or decremented before the value is received for use in the expression.
Postfix Increment and Decrement Operator
In postfix form, the previous value is obtained for the use in expression, and then the operand is modified.
Example
Here in the examples, there is no difference between the prefix and the postfix forms. However, when the increment and/or decrement operators are the part of a larger expression, then a subtle, however powerful, difference between these two forms appears. Here is an example:
x = 42; y = ++x;
Here, y is set to 43 as you would expect, as the increment occurs before x is assigned to y. Therefore, the line y = ++x; is equivalent of these two statements:
x = x + 1;
y = x;
Comments
Post a Comment