continue
In Python, the continue
keyword within loops lets you skip the rest of the code inside the loop for the current iteration and jump to the next iteration. When you use continue
, the loop doesn’t terminate. Instead, it skips the current iteration and moves to the next one.
Python continue
Keyword Examples
Here’s a quick example of how the continue
keyword works in a for
loop:
>>> for index in range(5):
... if index == 2:
... continue
... print(index)
...
0
1
3
4
In this example, the loop iterates over the range from 0
to 4
. When index
equals 2
, the continue
statement is executed, causing the loop to skip the call to print()
for that iteration and move to the next number in the range. As a result, 2
isn’t printed.
Python continue
Keyword Use Cases
- Skipping specific iterations in a loop when a certain condition is met
- Optimize performance by reducing unnecessary computations within loops
Related Resources
Tutorial
Python for Loops: The Pythonic Way
In this tutorial, you'll learn all about the Python for loop. You'll learn how to use this loop to iterate over built-in data types, such as lists, tuples, strings, and dictionaries. You'll also explore some Pythonic looping techniques and much more.
For additional information on related topics, take a look at the following resources: