In Python, break ends the nearest enclosing loop immediately, and continue skips the rest of the current iteration and jumps to the next one. Both work in for and while loops, and both affect only the innermost loop they appear in.
Use break when you have found what you were looking for or hit a stop condition. Use continue when a particular item should be ignored but the loop must keep going.
break exits a loop
continue skips an iteration
break and continue in nested loopselse clausebreak versus continue at a glancebreak exits a loopWhen Python executes break, it leaves the loop body at once. Execution resumes at the first statement after the loop. Any remaining items in the iterable are never visited.
The loop stops as soon as num is 5, so 5 is never printed. The same works in a while loop:
This prints 1 through 7 and then stops, even though the condition i <= 10 is still true.
breakThe most common use is early exit from a search, so you do not keep scanning after a match:
continue skips an iterationcontinue abandons the current pass through the loop body and moves straight to the next item (in a for loop) or back to the condition check (in a while loop).
Number 3 is skipped, but the loop carries on with 4 and 5. A typical real use is filtering bad input while processing a stream of records:
continue in while loopsIn a while loop, continue jumps back to the condition without running the code below it. If your counter update sits below the continue, you create an infinite loop:
Here the increment happens before the continue, so the loop always makes progress. Printing gives 1, 3, 4, 5.
break and continue in nested loopsBoth keywords act only on the innermost loop. A break inside the inner loop does not stop the outer loop.
Each row prints only columns 0 and 1, and the outer loop still runs three times. To leave several loops at once, wrap the loops in a function and use return, or raise and catch an exception for truly exceptional cases:
else clausePython loops accept an else block that runs only when the loop finishes without hitting break. It pairs naturally with search loops:
continue does not stop the else block from running; only break does. The else also runs when the iterable is empty.
break versus continue at a glance| Aspect | break | continue |
|---|---|---|
| Effect | Ends the whole loop | Ends only the current iteration |
| Next step | Statement after the loop | Next item or condition check |
Loop else block | Skipped | Still runs when the loop ends normally |
| Typical use | Search, stop condition | Filter or ignore items |
Neither keyword is allowed outside a loop; Python raises a SyntaxError. Also note that break and continue cannot be used in a finally block in older Python versions in some positions; since Python 3.8 continue is allowed there, and Python 3.14 emits a warning for break, continue and return that leave a finally block.
break exit all nested loops?No. It exits only the innermost loop that contains it. Use a function with return, or a flag checked by the outer loop, to leave several levels.
continue and pass?pass does nothing and execution proceeds to the next statement in the body. continue skips the remaining statements and starts the next iteration.
break in a list comprehension?No. Comprehensions have no statements. Use a normal loop, or itertools.takewhile to stop at a condition and a filter clause (if) to skip items.
while loop with continue never end?The code that updates the loop variable is probably after the continue. Move the update before it, or restructure the loop.
break stops the loop entirely and continue skips to the next iteration. Both apply to the innermost loop only, continue in a while loop needs the counter updated first, and the loop else block runs only when no break happened.