Here's a really simple example where continue actually does something measureable:
animals = ['dog', 'cat', 'pig', 'horse', 'cow']
while animals:
a = animals.pop()
if a == 'dog':
continue
elif a == 'horse':
break
print(a)
You'll notice that if you run this, you won't see dog printed. That's because when python sees continue, it skips the rest of the while suite and starts over from the top.
You won't see 'horse' or 'cow' either because when 'horse' is seen, we encounter the break which exits the while suite entirely.
With all that said, I'll just say that over 90%1 of loops won't need a continue statement.
1This is complete guess, I don't have any real data to support this claim :)
Answer from mgilson on Stack OverflowVideos
Here's a really simple example where continue actually does something measureable:
animals = ['dog', 'cat', 'pig', 'horse', 'cow']
while animals:
a = animals.pop()
if a == 'dog':
continue
elif a == 'horse':
break
print(a)
You'll notice that if you run this, you won't see dog printed. That's because when python sees continue, it skips the rest of the while suite and starts over from the top.
You won't see 'horse' or 'cow' either because when 'horse' is seen, we encounter the break which exits the while suite entirely.
With all that said, I'll just say that over 90%1 of loops won't need a continue statement.
1This is complete guess, I don't have any real data to support this claim :)
continue just means skip to the next iteration of the loop. The behavior here is the same because nothing further happens after the continue statement anyways.
The docs you quoted are just saying that you can only use continue inside of a loop structure - outside, it's meaningless.
https://pastebin.com/NWkKQc1P
It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?
PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.
while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms.
Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the break statement in the cmd == 'e' case of the sample in your question.
my question: while WHAT is True?
While True is True.
The while loop will run as long as the conditional expression evaluates to True.
Since True always evaluates to True, the loop will run indefinitely, until something within the loop returns or breaks.
what does the True refer to?
eg if i write:
while True:
print("hi")i know that "hi" will be repeated infinitely, but what is it thats True thats making "hi" get printed?
also, whatever it is thats True, im assuming its always true, so is that why if i typed 'while False', none of the code below would ever run?
sorry if this is a stupid question
edit: also, besides adding an if statement, is there anything else that would break this infinite while loop?
and how would i break the loop, lets say after "hi" has been printed 10 times, using an if statement or whatever else there is that can break it. thanks!