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".
Joined: Jan 30, 2002
Looking at this might help too:
This snippet will print: m is: 5 k is: 6
Joined: Dec 10, 2001
To repeat myself and others from previous conversations on similar topics, the real lesson here shouldn't be to understand exactly what happens in statements such as i = ++i or i = i++, but to learn to not write code in such a manner. Code should be clear to the humans that read it.