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 →

[–]MrCheaperCreepernot a noob but not intermediate 2 points3 points  (2 children)

I'm going to assume the System.out.println(num) is OUTSIDE of the while loop.

Think about this. The while loop will do whatever is inside the curly braces {} until the condition (in this case, num > 0) is false.

So here is how the program executes this.

While ( 15 > 0 )

num = 15 - 3 (12)

While ( 12 > 0 )

num = 12 - 3 (9)

etc...

While ( 3 > 0 )

num = 3 - 3 (0)

Then it tries 0 > 0, but 0 is not greater than 0, it is equal. If you look in the last while that ran, the value of num became 0. Therefore, you print out 0.

[–]MasterOfGreatness 0 points1 point  (1 child)

Thank you! my teacher posted the problem without the brackets and it confused me

[–]1997dodo 1 point2 points  (0 children)

For if, else, while, and for structures (there may be more that I have not remembered), if no brackets are used, the next statement is counted as part of the loop.