Python•Loop Control
Python Break, Continue, and Pass Loop Control Statements
break immediately terminates the enclosing loop. continue skips the rest of the current iteration and jumps to the next cycle. pass does nothing.
Syntax
break # Exit loop immediately
continue # Skip to next iteration
pass # Null placeholder
Parameters & Values
Key components and acceptable values for this structure:
| Parameter | Type | Description |
|---|---|---|
| break | Keyword | Exits loop completely. |
| continue | Keyword | Skips remainder of current iteration. |
| pass | Keyword | Syntactic placeholder for empty code blocks. |
Ready-to-Run Code Example
Executable in CompilraRun this complete, working example directly without installing Python locally:
# Demonstrating break and continue
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Printing odd numbers up to 7:")
for n in numbers:
# Skip even numbers
if n % 2 == 0:
continue
# Stop when number exceeds 7
if n > 7:
break
print(f"-> {n}")Expected Output:
Printing odd numbers up to 7: -> 1 -> 3 -> 5 -> 7
How It Works: Step-by-Step
- Lines 7-8: continue skips even numbers 2, 4, 6, 8.
- Lines 10-11: break halts loop execution as soon as n reaches 9.
Key Concepts
- continue skips straight to the next sequence item without running subsequent lines.
- break terminates the innermost loop cleanly.
- Loops can feature an optional else: block which executes only if break was not triggered.
Common Mistake: Placing code after break within the same branch
Statements placed immediately after break in a block are unreachable.
❌ Incorrect
if item == target:
break
print("Found!") # Unreachable code✅ Correct
if item == target:
print("Found!")
breakRelated Python Tutorials & Compiler
Python For Loops and the range() Function
A for loop in Python is used for iterating over a sequence (such as a list, tuple, string, or range).
Python While Loops: Syntax, Execution, and Conditions
A while loop repeatedly executes a target statement block as long as a given boolean condition remains True.
Python If...Else Conditional Statements with Examples
Decision making in Python is performed using if, elif, and else statements to execute code blocks based on boolean conditions.
Python Online Compiler
Open in Compilra IDE
Run and test Python on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.