i++ (and ++i ) is performed as part of the evaluation of the while expression that occurs before printing. Thus, this means that it will always print 1 initially.
The only difference between the i++ and ++i variants is that the increment occurs inside the expression itself, and this affects the final value printed. For each equivalent pseudo-code:
while(i++ < 10) while i < 10: i = i + 1 printf("%d\n", i); print i i = i + 1
and
i = i + 1 while(++i < 10) while i < 10: printf("%d\n", i); print i i = i + 1
Say i gets up to 9 . Using i++ < 10 it uses 9 < 10 for the while expression, and then i prints to 10 before printing. Thus, the check uses 9, then prints 10.
C ++i < 10 it first increments i , and then uses 10 < 10 to express while . Thus, the check uses 10 and does not print anything, because it exited the loop from this check.
source share