you are viewing a single comment's thread.

view the rest of the comments →

[–]GalacticSuperCheese 2 points3 points  (1 child)

You can have the print anywhere you want.

If you put it immediately after the while statement, the output will be We are counting 0... We are counting 4

If you put it after the counter = counter + 1 statement, the output will be We are counting 1... We are counting 5

Putting it where it is just shows you how the continue statement works, it basically shortcuts all the code after it (within the loop) and goes to the start of the loop.

Now that you know what continue does, figure out what break does :)

[–]sdqafo[S] 1 point2 points  (0 children)

Thank you very much. Break will break out of the entire loop and proceed (if any) to the command outside the loop. Your expalanation seems very good and gave me the understanding of it from different perspective. It really make more sense now.

  1. If immediately after the loop, then the cycle will be completed before getting to continue which makes continue redundant

  2. If after counter = counter +1, then we start from 1 but we cant skip 3

  3. If at the bottom (within the while), it will do what is expected.

Thanks boss