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 (Definite Iteration)
In this introductory tutorial, you'll learn all about how to perform definite iteration with Python for loops. You’ll see how other programming languages implement definite iteration, learn about iterables and iterators, and tie it all together to learn about Python’s for loop.
For additional information on related topics, take a look at the following resources:
- Python "while" Loops (Indefinite Iteration) (Tutorial)
- For Loops in Python (Definite Iteration) (Course)
- The Python for Loop (Quiz)
- Mastering While Loops (Course)
- Python "while" Loops (Quiz)