Featureintro
Break and continue Statements
Also called break statement, continue statement, exit a loop early, skip an iteration, break out of a for loop.
break ends the innermost for or while loop at once and skips the else clause of the loop. continue skips the rest of the loop body and starts the next pass. Both work only inside a loop.
- Language
- Python
Synopsis
for n in range(1, 8):
if n % 2 == 0:
continue
if n > 5:
break
print(n)
1
3
5
continue skips the even numbers. break ends the loop at 7.
Semantics
r1. "The break statement breaks out of the innermost enclosing for or while loop".1 "It terminates the nearest enclosing loop, skipping the optional else clause if the loop has one."2 Example: inner-only.
r2. "The continue statement continues with the next iteration of the loop".1 Example: both.
r3. Each of the two statements "may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop."2 Example: break-in-function.
r4. "If a for loop is terminated by break, the loop control target keeps its current value."2 Example: keeps-value.
r5. "When break passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the loop." The rule for continue is the same: the finally clause runs "before really starting the next loop cycle".2 Example: finally.
Examples
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(f"{n} equals {x} * {n//x}")
break
4 equals 2 * 2
6 equals 2 * 3
8 equals 2 * 4
9 equals 3 * 3
This is the example of the tutorial. The break ends only the inner loop, and the outer loop goes on with the next n (rule r1).
for i, name in enumerate(["ann", "bob", "cid"]):
if name == "bob":
break
print(i, name)
1 bob
for i in range(3):
try:
if i == 1:
continue
if i == 2:
break
print("body", i)
finally:
print("finally", i)
body 0
finally 0
finally 1
finally 2
Pitfalls
A break in a function that is defined in a loop body is not in a loop:
This code raises SyntaxError, on purpose.
for i in range(3):
def stop():
break
Use a return value or an exception to tell the loop to stop.
break ends only one loop. To leave two nested loops at once, see the techniques for exiting nested loops.
Comparison
In Go, a break statement "terminates execution of the innermost" for, switch or select statement "within the same function".3 So a Go break in a switch ends the switch, not the loop. A Python break in a match statement ends the loop, because match is not a loop (rule r1). A Go break or continue can also name the label of an enclosing statement, "and that is the one whose execution terminates."3 PEP 3136 proposed labels for break and continue in Python, and its status is "Rejected".4 So a Python break always ends the innermost loop.
References
- The Python Tutorial, 4. More Control Flow Tools, 4.4. break and continue Statements
- The Python Language Reference, Python 3.15, 7.9. The break statement; 7.10. The continue statement
- The Go Programming Language Specification, Break statements; Continue statements
- PEP 3136 - Labeled break and continue, Status; Rejection Notice
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag S4YSAT Edit this page History Report an error Markdown