Loop control: continue, break, and skipping invalid input
Inside any loop, two keywords change the flow. break leaves the loop entirely. continue skips the rest of the current pass and jumps straight back to the next item — the loop keeps going.
continue shines in the validate-and-skip pattern: walk a collection, and for any item that fails a check, continue past it; only items that survive reach the code below. This reads as "reject early, then handle the good case" — and it keeps the rest of the loop body un-indented, which is much easier to follow than burying the real work inside a deep if.
A neat consequence: once execution gets *past* the continue, you know the item is valid. So you can do if v <= 0: continue then treat everything below as positive — no extra nesting.
Real programs use this to ignore blank lines, skip malformed records, drop out-of-range values — anything where "this one isn't worth processing, move on" is the right call instead of crashing.
Syntax
for item in items:
if not is_valid(item):
continue # skip this item, go to the next
process(item) # only valid items reach here
# break leaves the loop entirely:
for item in items:
if found(item):
breakWorked examples
Skip the non-positive, return the first good one
def first_positive(values):
for v in values:
if v <= 0:
continue # skip zeros and negatives
return v # past the continue ⇒ v is positive
return None # nothing positive found
print(first_positive([0, -3, 5, 2])) # 5
print(first_positive([0, -1, -99])) # NoneValidate-and-skip: keep only the clean records
def clean(values):
result = []
for v in values:
if not isinstance(v, int):
continue # ignore anything that isn't an int
result.append(v)
return result
print(clean([1, "x", 2, None, 3])) # [1, 2, 3]continue vs break — skip one vs stop all
kept = []
for n in range(1, 6):
if n == 3:
continue # skip just 3
if n == 5:
break # stop the whole loop at 5
kept.append(n)
print(kept) # [1, 2, 4]Common mistakes & gotchas
continue skips, break stops
It's easy to mix them up. continue abandons only the current pass and carries on with the next item; break ends the loop completely. Reaching for the wrong one either processes too much or too little.
continue can skip a needed update in a while loop
In a while loop, if you continue *before* the line that advances the counter, you skip that advance and can loop forever. In a for loop this can't happen (the iterator advances for you), but in while make sure the counter is updated before any continue.
An empty loop body still needs something
If you want a loop placeholder that does nothing, that's pass, not continue. pass is "do nothing and carry on normally"; continue is "jump to the next iteration". They are not interchangeable.
Why it matters
Real input is messy — blank lines, junk values, half-broken records. The validate-and-skip loop is how robust programs cope: ignore what they can't use and keep going, rather than crashing on the first bad item. `continue` keeps that logic flat and readable instead of a pyramid of nested ifs.