All courses › Programming with Python › break and continue

break and continue

break ends the loop immediately, while continue just skips to the next round and keeps the loop going. Both are typically used with an if condition inside the loop to handle special cases.

break\text{break}ends the whole loop immediately
continue\text{continue}skips the rest of the round, loop continues

Example

for i in range(3): pass

print(i) prints 2, since the loop variable keeps its last value after the loop.

break leaves the loop, continue stays in it — do not mix them up.
Practise loops and conditions for free →

← Conditionals: if/elif/else · List comprehension →

Part of Programming with Python: Loops and conditions.