Three statements for steering a loop mid-flight โ stop it entirely, skip one round, or do nothing at all.
Python provides three special statements to control the flow of loops (for and while):
break โ exits the loop entirely, skipping any remaining iterations.continue โ skips the rest of the current iteration and moves to the next one.pass โ does nothing; it's a placeholder that lets you write a syntactically valid empty block.All three examples in this note use the same base loop: for i in range(1, 11):, which iterates i from 1 to 10.
break terminates the entire loop as soon as the condition is met โ no further iterations run, even if there were more values left.continue skips only the current iteration โ the loop keeps running for the remaining values.pass is a no-op statement โ used when Python syntax requires a statement (e.g., inside an if, loop, or function body) but you don't want any code to run there yet.continue is triggered, every statement after it in that loop iteration is skipped, not just the next line โ execution jumps straight back to the top of the loop.for i in range(1, 11):
if i == 5:
break
print(i)
Output:
1
2
3
4
What happens: The loop prints i normally for 1, 2, 3, 4. When i becomes 5, the if condition is True, so break runs and the loop stops immediately โ it never reaches 6, 7, 8... even though range(1, 11) technically goes up to 10.
Note: break only exits the innermost loop it's placed in.
for i in range(1, 11):
if i == 5:
continue
print(i)
Output:
1
2
3
4
6
7
8
9
10
What happens: When i == 5, continue is triggered, so print(i) is skipped only for that one iteration (5 is never printed). The loop then moves on and continues normally for 6 through 10.
for i in range(1, 11):
if i == 5:
continue
print(i)
print("Hello")
Output (pattern):
1
Hello
2
Hello
3
Hello
4
Hello
6
Hello
7
Hello
8
Hello
...
What happens: For every value of i except 5, both print(i) and print("Hello") run. But when i == 5, continue skips both remaining print statements for that iteration โ not just the next line โ and jumps straight to i = 6. This demonstrates that continue skips everything below it in the loop body for the current pass, not just the immediately following statement.
for i in range(1, 11):
pass
What happens: This loop runs 10 times but does nothing on each iteration โ pass is just a placeholder. There is no output. It's typically used as a temporary stand-in while writing code, e.g., when you've defined a loop or function structure but haven't decided what should go inside yet.
| Statement | Effect | Scope of effect |
|---|---|---|
break | Exits the loop completely | Entire loop (remaining iterations don't run) |
continue | Skips to the next iteration | Only the current iteration |
pass | Does nothing (placeholder) | No effect on control flow |
break and continue are typically used inside an if condition within a loop.break stops the loop immediately, even mid-iteration.continue does not stop the loop โ it only skips the remainder of the current cycle.pass is purely syntactic โ it never affects output or control flow, it just satisfies Python's requirement that a block can't be empty.SyntaxError: unexpected EOF while parsing โ this is an editing artifact, not a language issue with pass.break = stop the whole loop right now.continue = skip the rest of this round, keep looping.pass = do nothing, just a placeholder to keep the syntax valid.range(1, 11) and if i == 5: break โ stops after printing 1-4; continue โ prints 1-4, skips 5, then prints 6-10.continue skips all code below it in that loop cycle, not just one line.break stops the whole loop right now. continue skips the rest of this round and keeps looping. pass does nothing โ it's just a placeholder to keep the syntax valid. With range(1, 11) and if i == 5: break stops after printing 1-4, while continue prints 1-4, skips 5, then prints 6-10. Remember that continue skips all code below it in that loop cycle, not just the next line.