This guide explains continue Statement: Skip an Iteration through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
continue skips the rest of the current iteration and begins the next one. It is useful for filtering invalid items without increasing indentation for the main processing logic.
When You Would Use This
Ignore negative readings in a simple report.
Step-by-Step Example
readings = [18, -1, 21, -3, 20]
for reading in readings:
if reading < 0:
continue
print("Accepted:", reading)
How the Example Works
The body prints only 18, 21 and 20. Negative values skip directly to the next reading.
Example Output
Accepted: 18
Accepted: 21
Accepted: 20
Try It Yourself
Add a counter for rejected negative readings and display it after the loop.
Common Beginner Mistakes
- Confusing
continuewithbreak, which ends the loop. - Skipping necessary counter updates in a
whileloop and creating an infinite loop. - Using
continueto hide invalid data that should be reported.
Key Takeaways
continueskips the rest of the current iteration and begins the next one.- The body prints only 18, 21 and 20.
- Avoid this mistake: Confusing
continuewithbreak, which ends the loop.