The continue statement ignores the rest of the loop and returns back to the top for the next iteration.
In your example count is 0 at the beginning. Since it is not equal to 5, the if statement is not executed and count is printed in incremented by one. When count finally reaches 5 after 5 iterations or the loop, the if statement is executed. Since the only instruction is continue, the print and increment are never executed: the rest of the loop body is ignored. After this point count always has a value of 5 and this state continues indefinitely.
It does not break the loop, the loop is still running forever, doing nothing.
count = 0
while count < 15:
if count == 5:
continue
# The following is ignored after count == 4
print(count)
count += 1
Answer from Louis Lac on Stack OverflowThe continue statement ignores the rest of the loop and returns back to the top for the next iteration.
In your example count is 0 at the beginning. Since it is not equal to 5, the if statement is not executed and count is printed in incremented by one. When count finally reaches 5 after 5 iterations or the loop, the if statement is executed. Since the only instruction is continue, the print and increment are never executed: the rest of the loop body is ignored. After this point count always has a value of 5 and this state continues indefinitely.
It does not break the loop, the loop is still running forever, doing nothing.
count = 0
while count < 15:
if count == 5:
continue
# The following is ignored after count == 4
print(count)
count += 1
I think you need to use a pass statement instead of continue and change your indentation (this is assuming you want to print numbers from 0-15 but not 5).
pass is the equivalent of doing nothing
count = 0
while count <15:
if count == 5:
pass
else:
print(count)
count += 1
continue takes the code to the end of the loop. This means that when count is 5, the loop goes to the end and the value of count never increments and gets stuck in an infinite loop.
Take a look at break, pass and continue statements
Can't understand "Continue" function in Python statement(for loop, while loop) !!
How continue works in while loop
python - Is the continue statement necessary in a while loop? - Stack Overflow
What is the most pythonic way to limit while loop iterations?
Videos
I am new to learning Python or rather I say new to programming. So, anyway
Can anyone help me understanding the "continue" function in Python statement(for loop while loop etc) or help me with a link with broad explanation?
I get "break" , "pass" but can't quite understand the use of "continue".
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.