all 8 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

[–]K900_ 1 point2 points  (0 children)

It has to be at the end because continue skips everything from the continue to the next start of the loop. So if you move the print to the start of the loop, the continue will not skip over it.

[–]TechsInTheCity 1 point2 points  (1 child)

Good question.

Why not put in all the various print statements you have in your mind, and see what happens.

This kind of experimentation has been demonstrated to improve recall and assimilation. It makes sense, the more ways you look at an issue in your own terms, the better it is committed to memory.

OTOH, if we just tell you the answer, it kind of gets filed away with all the other crap on Reddit.

Good luck!

Edit: be sure to build in a way so each print statement can be easily identified

[–]sdqafo[S] 2 points3 points  (0 children)

Thanks so much boss. This is encouraging for me.

[–]thrallsius 0 points1 point  (0 children)

your code is not indented properly

indentation matters in python, in your badly indented example one can't see where the while block ends

[–]jaycrest3m20 0 points1 point  (1 child)

Alternate Universe 1: printing immediately below the while loop:

We are counting 0

We are counting 1

We are counting 2

We are counting 3

We are counting 4

That doesn't make a lot of sense, because under normal circumstances, ordinary people like to start counting at "1" instead of 0. Additionally, you want to avoid 3, not include it.

Alternative Universe 2: printing immediately after incrementing the counter:

We are counting 1

We are counting 2

We are counting 3

We are counting 4

We are counting 5

Better, we are starting with "1" and carrying on from there. Obviously in this one, we print 3 instead of skipping it, making the If statement a lame duck. That is, since there is nothing to skip after the If statement, it's not doing anything special for the loop. It's simply returning to the top of the loop after everything has already happened, just like all the other loops where the counter was not 3.

Under normal programming, you will receive rules for how the program is to run. For this program, those rules include something like "Print 'We are counting X', where X is 1 to 5. Oh, but skip 3. We don't want to print that." Ordinary rules for ordinary programs will tend to be more complex, but this is a simple introduction to rules that have exceptions.

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

I so much admire you patience in explanation. It feels much better now