After evaluating i++
or ++i
, the new value of i
will be the same in both cases. The difference between pre- and post-increment is in the result of evaluating the expression itself.
++i
increments i
and evaluates to the new value of i
.
i++
evaluates to the old value of i
, and increments i
.
The reason this doesn’t matter in a for loop is that the flow of control works roughly like this:
- test the condition
- if it is false, terminate
- if it is true, execute the body
- execute the incrementation step
Because (1) and (4) are decoupled, either pre- or post-increment can be used.