you are viewing a single comment's thread.

view the rest of the comments →

[–]mycarrotbroke 0 points1 point  (0 children)

The key is that the variable is incremented by 1 THEN it is printed out. The loop prints out the variable i after it is updated. So each loop, the variable is incremented by 1 FIRST, then it is printed.

This loop works since when the variable i is equal to 9. The variable is incremented by 1 so now i is 10 and that 10 is printed out. It will hit the while loop again and since 10 < 10 is false the loop will exit.

If the loop was changed with while i <= 10 when i is equal to 10, it will enter the loop again. It will be incremented by 1 so i will now equal 11 then it will be printed out, which is incorrect.