continue
Python Keywords
Reference ยท continue
โญ๏ธ Skipping to the Next Pass
Where break ends a loop, continue only ends the current pass and lets the loop carry on. It is the tool for “ignore this one”: a blank line, an invalid entry, a record that fails a check. This page covers how continue behaves in for and while loops, the one trap that can turn it into an infinite loop, how it works with nested loops, the loop else and try/except, and how it works alongside break. Every example was run, and the output shown is the real output.
continue skips the rest of the loop body and moves on to the next pass in Python, Java and JavaScript. Python has no labelled continue (continue outer;), so it always applies to the innermost loop. In JavaScript, forEach callbacks have no continue: you return early from the callback instead. One difference that matters more in Python: loops have an else block, and continue does not prevent it from running; only break does.
continue past rows that fail validation while collecting their errors, then return the good results and a list of problems in one response (Example 6), instead of failing the whole request because of one bad row. FastAPI's own request validation uses Pydantic for the same job, but plain loops like this are what you will write for anything it does not cover.
๐งฐ The Building Blocks
Eight pieces. The first two are the everyday uses, the third is the trap, and the rest are how it behaves around other features.
break, which ends it.while loop, continue jumps back to the top and the condition is checked again. A good fit for “that was not valid, ask again”.while loop, anything after the continue is skipped, including the line that updates the loop variable. If that line is skipped, the condition never changes and the loop never ends.continue says “not this one” and gets rid of a case early, so the main work stays at one indentation level. Compare the two versions: same output.break, continue belongs to the nearest loop around it. Here it skips column 1 on every row, and the outer loop is not affected.else runs when the loop ends without a break. A continue is not a break, so the else still runs.try only runs for good values.if says the same thing in one line. Keep continue for loops that do real work with each item.๐ How It Works, Step by Step
- The loop runs its body normally.
- When Python reaches the
continueline, it abandons the rest of this pass. Nothing after thecontinuein the loop body runs. - In a
forloop, control moves to the next item (or ends the loop if there are none left). In awhileloop, control goes back to the top and the condition is tested again. - The loop is not over. That is the whole difference from
break.
When to reach for it: whenever some items should be ignored and you would otherwise have to wrap the whole body in an if: skipping blank lines, invalid entries, items that fail a check, or input that should be asked for again.
๐งช In Context
Real uses of continue, including the guessing game from the break page and a loop that uses both keywords together.
# are not data, so continue throws them away and the three lines that follow only ever see real settings.continue can skip it: here i += 1 is the first line of the body.break page. A typo or an out-of-range number should not use up one of the five attempts. continue asks again without touching the counter, and break still ends the game on a correct guess. The two keywords work side by side.Look at the order inside the loop. The counter is only increased after the validation check, so a continue before that line leaves it alone. Moving attempts += 1 to the top of the loop would charge an attempt for every typo.
done. break is the exit; continue rejects an entry that is not a number or is negative and asks again. Neither bad entry changes the total.pass is a placeholder that does nothing at all, so the rest of the body still runs.continue keeps the good ones flowing, and the bad ones are collected so they can be reported at the end, a pattern you will use whenever a whole batch should not fail because of one bad row.๐ Quick Reference
| What you want | Write | Notes |
|---|---|---|
| Skip the rest of this pass | continue | Works in for and while. The loop itself carries on. |
| Skip items that fail a check | if bad: continue | Keeps the main work at one indentation level. |
| Ask again after invalid input | continue inside while True | Do the validation before the counter is updated. |
| Skip a failed conversion | except ValueError: continue | The code after the try only runs for good values. |
| Keep a counter safe in a while loop | update it before the continue | Otherwise the loop may never end. |
| End the loop instead of skipping | break | See the break page. |
| Do nothing, keep the rest of the body | pass | A placeholder, not a loop control. |
| Filter into a new list | [x for x in xs if cond] | Replaces a filter-only loop with a continue. |
| continue outside any loop | SyntaxError | Raised before the program runs at all. |
continue in a while loop can skip the update: if the line that changes the loop variable (i += 1) comes after the continue, it is skipped and the loop never ends. In a for loop this cannot happen, because for advances itself. Update first, or use a for.continue is not break: break ends the loop; continue only ends the current pass. If the loop stops when you expected it to carry on (or the other way round), check which one you wrote.There is no labelled
continue: Java and JavaScript allow continue outer; to jump to the next pass of an outer loop. In Python, continue always belongs to the innermost loop. To skip to the next outer item, break out of the inner loop (and check a flag), or put the inner part in a function that returns.JavaScript's
forEach has no continue: inside the callback you use return to skip an item. A Python for loop is a real loop, so continue and break both work directly.The loop
else still runs after a continue: only break prevents it. If the else is meant to mean “nothing was found”, make sure a continue is not hiding the case you care about.Do not overuse it: several
continue lines in a row can make a loop harder to follow than a plain if. For a simple filter, a list comprehension is clearer, and a loop with one or two guard clauses is fine.Put validation before side effects: anything that changes state (a counter, a total, a list) should come after the checks that may
continue. Otherwise a rejected item has already left its mark, as in Example 3 where the attempt counter would have been charged for every typo.