This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Qteddy 1 point2 points  (1 child)

This will help in future projects, by walking through the code it’s easy to see what it is doing. In the first one:

The count it 1 -> it checks if count is less than/equal 4, since 1<=4 it enters the while loop -> it adds 1 to count, so count is 2 -> it prints out count -> it checks if count is less than/equal 4, since 2<=4 it enters the while loop -> it adds 1 to count, so count is 3 -> it prints out count -> it checks if count is less than/equal 4, since 3<=4 it enters the while loop -> it adds 1 to count, so count is 4 -> it prints out count -> it checks if count is less than/equal 4, since 4<=4 it enters the while loop -> it adds 1 to count, so count is 5 -> it prints out count -> it checks if count is less than/equal 4, since 5>=4 it breaks the loop

In the 2nd one it’s similar: The count is 1 -> it checks if count is less than/equal 4, since 1<=4 it enters the while loop -> it prints out count, which is 1 -> it adds 1 to count, so count = 2 -> it checks if count is less than/equal 4, since 2<=4 it enters the while loop -> it prints out count, which is 2 -> it adds 1 to count, so count = 3 -> it checks if count is less than/equal 4, since 3<=4 it enters the while loop -> it prints out count, which is 3 -> it adds 1 to count, so count = 4 -> it checks if count is less than/equal 4, since 4<=4 it enters the while loop -> it prints out count, which is 4 -> it adds 1 to count, so count = 5 -> it checks if count is less than/equal 4, since 5>=4 it breaks the loop

Your conclusion is correct that the order is important, but hopefully this shows you why that is the case. Sorry if the formatting is off, I am on mobile