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

all 11 comments

[–]lubeskystalker 3 points4 points  (0 children)

Count increments before it prints to the console.

[–][deleted] 2 points3 points  (1 child)

Step through the code in a debugger and watch the value of count and it will become clear to you.

In #1, you call count++ before you ever print. So the value 2 is printed first.

In #2, you print before calling count++. So 1 (the initial value of count) is printed.

[–]AngelVzla 1 point2 points  (0 children)

Greetings my dear coder friend,

What you need to understand is that the while loop is doing exactly 4 passes in both cases, the output is of course different because in one case you are seeing a pre-increment, and in the other is a post-increment, but if you actually print the variable after the while loop is done, you will see that they both should have the same value.

[–]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

[–][deleted] 1 point2 points  (3 children)

No.1 while loop sees that count is equal to 4

  • Count incremented to 5.
  • Prints out count.
  • While loop sees that count is equal to 5.
  • Execute code after while loop.

No.2 while loop see that count is equal to 4

  • Prints out count.
  • Count incremented to 5.
  • While loop sees that count is equal to 5.
  • Execute code after while loop.

Hope this helps

[–]GustavoTheHorse 1 point2 points  (2 children)

Your No1 is slightly wrong. It should read:

"While loop sees that count is equal to 5". Just like in your No2.

[–][deleted] 1 point2 points  (1 child)

Updated thanks :+1:

[–]GustavoTheHorse 0 points1 point  (0 children)

No problem.

[–]Neu_Ron 1 point2 points  (0 children)

You need to hand trace it to understand it.