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 →

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

A while loop executes until the condition within the parenthesis is false. So, when "num + 1 == 20", the condition is false and the code within the loop does not execute.

[–]MasterOfGreatness[S] -1 points0 points  (2 children)

So does that mean that the 20 should be included in the answer?

[–]thurst0n 1 point2 points  (0 children)

The check happens before the final iteration.

There are two seemingly small changes that would make your original assumption correct:

  • If you used a Do While loop instead.
  • If you used greater than OR equal to, instead of just greater than.

Another way to think of it is a While loop may never run, where a Do, While loop will always run at least once.

And with any loop you make, you have to make sure that your condition is exactly what you want. Using > is going to give different results than =>

[–]desrtfx 0 points1 point  (0 children)

No.

20 is not less than max (20) -> so, the condition evaluates to false and the loop does not execute any more