posted 18 years ago
If the statement was this instead:
i = i++;
Then that makes sense...it's just a matter of operator precedence. The last thing this statement actually does is set i to the pre-incremented value of i++. The actual value of i is then 0.
In other words:
i on the right hand side evaluates to 0 during this expression. It is not incremented until after the expression completes.
However the = operator has a lower precedence than the ++ operator.
So the ++ changes i = 1
But then the = operator assigns i = 0, the value that i is throughout the expression. Remember it doesn't get incremented until after the expression is completed. However the increment is lost since you performed an assignment (lower precedence).
Confusing, I know. Hard for me to explain. That's why I rarely use postfix or prefix increments within expressions. I typically use them "stand-alone".