Continue statement is used when you want to skip the loop for a certain condition and continue iterating with the next item on the list. Continue doesn’t break the loop like the break statement does, instead, it skips the current iterating element and continues with the next item on the iteration.
Continue statement must be used inside a loop block and it is generally used after an if conditional statement.
Using Continue Statement In Python
Here is a sample code snippet demonstrating usage of continue statement in Python:
for fruit in ["apple", "banana", "kiwi", "strawberry"]: if fruit == "banana": continue # skip this item print('I <3 ' + fruit) print('The end.')
This sample code snippet outputs the following:
I <3 apple I <3 kiwi I <3 strawberry The end.
Note that, the banana item on the list is skipped using the continue operator.
I hope you liked this tip. Don't forget to follow me on twitter for more tips like this ;)