Module 6 · Loops and Iteration — topic 8 of 12
continue
continue skips the rest of the current iteration and moves to the next one. Learn how it simplifies filtering inside loops, its effect in while loops, and how to avoid skipping the update step.
Learning objectives
- Skip items with continue
- Use continue to reduce nesting
- Avoid infinite loops caused by continue in while
Definition
continue skips the rest of the current iteration and moves to the next one. Learn how it simplifies filtering inside loops, its effect in while loops, and how to avoid skipping the update step.
Introduction
continue ends the current iteration early and jumps straight to the next one. In a for loop that means the next item; in a while loop it means checking the condition again.
Why it matters
Data often contains items you want to ignore: blank lines, invalid records, comments. continue lets you skip them at the top of the loop body and keep the main processing code unindented and readable.
Detailed explanation
Statements after continue in the same iteration are not executed.
In a for loop, continue moves to the next item automatically.
In a while loop, continue jumps back to the condition. Make sure the loop variable has already been updated, or the same value will be processed forever.
continue is like a guard clause for loop bodies: if not wanted: continue, then the normal processing follows.
Many continue-based filters can also be written as comprehensions or with an if around the body; choose whichever reads most clearly.
continue only affects the innermost loop. In nested loops it moves the inner loop on to its next item; the outer loop is unaffected.
When data is skipped, it is good practice to count or log what was skipped. A silent skip can hide bad input that someone should fix, while a count such as “3 rows rejected” makes the problem visible.
Syntax
- continue is usually inside an if.
- The rest of the body is skipped for that item.
- The loop carries on with the next item.
Code examples
Skipping odd numbers
2 4 6
Ignoring blank and comment lines
name -> Asha city -> Pune
More examples
Cleaning sales data
Total: 495 Rejected: 3 processed 1 processed 2 processed 4 processed 5
Step-by-step explanation
- Non-numeric text (abc and the empty string) is counted as rejected and skipped.
- Negative amounts are rejected by a second continue.
- Only valid amounts reach total += amount: 120 + 300 + 75 = 495.
- In the while loop, i is increased before the continue, so skipping 3 can’t cause an infinite loop.
Expected output
Running “Cleaning sales data” prints exactly:
Practical applications
- Log analysers skip lines that don’t match the pattern they need.
- CSV importers skip header or malformed rows.
- Email filters skip messages already processed.
Best practices
- Put skip conditions at the top of the loop body.
- Update while-loop variables before any continue.
- Count or log skipped items when processing data.
Common mistakes
continue before the update in while
Avoid
Prefer
If the update comes after continue, i stays 2 forever.
Using continue where break was meant
Avoid
Prefer
continue only skips one iteration; break stops the loop.
Key takeaways
- continue skips the rest of the current iteration.
- In while loops, update the variable before continuing.
- Use it to filter items and reduce nesting.
Try it yourself
- 1Print the numbers 1–20 except multiples of 4, using continue.
- 2Sum a list of strings, skipping any that aren’t digits.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?