In Python, break ends the innermost loop immediately and continues with the first statement after it. continue only 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 loop they appear in directly.
Use break when you have found what you were looking for or can no longer continue. Use continue when a single item should be skipped but the loop must keep going.
When Python executes break, it leaves the closest enclosing for or while loop at once. Any remaining items are never visited, and the loop's else clause (if it has one) does not run.
The loop would normally run ten times, but break stops it as soon as i is 3. Execution resumes at the print("loop finished") line.
continue abandons the current pass through the loop body and moves on. In a for loop the next item is taken from the iterator. In a while loop the condition is evaluated again.
The value 2 is never printed because continue skipped the print call, but the loop still processed 3 and 4.
| Aspect | break | continue |
|---|---|---|
| Effect | Leaves the loop completely | Skips to the next iteration |
| Remaining items | Not processed | Still processed |
Loop else block | Does not run | Still runs when the loop ends normally |
| Typical use | Search found, error, stop condition | Filtering out unwanted items |
| Works in | for and while | for and while |
For a plain membership test, target in fruits is shorter. break pays off when each item needs real work before you know whether it matches.
Using continue as a guard at the top of the loop keeps the main logic unindented, which is often easier to read than wrapping everything in a large if block.
Here continue ignores empty lines and break ends an otherwise infinite while True loop.
A for or while loop can have an else block. It runs only when the loop finishes without hitting break. That makes it a clean way to express "not found".
A continue never prevents the else block from running, because the loop still ends normally.
break only leaves the innermost loop. To leave several levels, the usual options are a function with return, or a flag checked by the outer loop.
Wrapping the nested search in a function and returning is normally cleaner than a flag variable, and it also makes the code easy to test.
continue in a while loop before updating the loop variable. The condition never changes, so the loop runs forever. Update the counter first or use a for loop.break to leave an outer loop. It only exits the loop it is written in.SyntaxError.break or continue inside a finally block. It works, but it can swallow exceptions and is discouraged. Recent Python versions (3.14 and later) warn about control flow that leaves a finally block.break in a match statement. Unlike some languages, match does not need it, and break there still refers to the enclosing loop.No. continue only skips the current iteration. The else block runs when the loop ends normally, and only break prevents it.
Yes. They are almost always used inside an if. They apply to the nearest enclosing loop, not to the if.
Put the loops in a function and use return, or set a flag and check it in the outer loop. Python has no labelled break.
Yes. pass does nothing and the rest of the loop body still runs. continue skips the rest of the body for that iteration.
break terminates the loop, continue skips only the current iteration. Combine break with the loop else clause to handle the not found case, return from a function to leave nested loops, and be careful with continue in while loops so the loop condition still changes.