Both continue and pass can appear to "do nothing" in a code block, but they serve completely different purposes. One controls loop execution flow, while the other is purely a syntactic placeholder.
Understanding when to use each prevents subtle bugs and makes your intentions clear to other developers.
Quick Summary
| Keyword | Function | Effect | Context |
|---|---|---|---|
pass |
Placeholder | Does nothing. Execution proceeds to the immediate next line. | Functions, Classes, Loops, Ifs |
continue |
Control Flow | Skips the rest of the current iteration and jumps to the next loop cycle. | Loops Only (for/while) |
Using pass (The Placeholder)
Python syntax requires code blocks (functions, classes, conditionals) to contain at least one statement. Use pass to satisfy this requirement without executing any logic:
class CustomError(Exception):
pass
def complex_algorithm():
pass # TODO: Implement this later
def process(value):
if value < 0:
pass # Negative values need no action
else:
print(f"Processing {value}")
try:
result = risky_operation()
except KeyboardInterrupt:
pass # Silently ignore Ctrl+CUsing continue (The Skipper)
The continue keyword works only inside loops (for or while). It immediately ends the current iteration and jumps to the next one:
users = ["Alice", "Bob", "Admin", "Charlie"]
for user in users:
if user == "Admin":
print("Skipping admin account...")
continue # Jump to next iteration
print(f"Sending email to {user}")Output:
Sending email to Alice
Sending email to Bob
Skipping admin account...
Sending email to CharlieCommon Use Cases for continue
for row in data:
if not row.is_valid():
continue
process(row)
for line in file:
if line.startswith('#'):
continue
parse(line)
for item in items:
if item is None:
continue
if item.status != 'active':
continue
handle(item)Side-by-Side Comparison
Observe how behavior differs when swapping pass and continue:
With pass:
for i in range(3):
if i == 1:
pass # Does nothing, execution continues
print(i)Output:
0
1
2With continue:
for i in range(3):
if i == 1:
continue # Skips to next iteration
print(i)Output:
0
1
2Visual Flow Diagram
pass: continue:
┌─────────────┐ ┌─────────────┐
│ if i == 1 │ │ if i == 1 │
│ pass │ │ continue ─┼──┐
└──────┬──────┘ └─────────────┘ │
│ │
▼ │
┌─────────────┐ ┌─────────────┐ │
│ print(i) │ │ print(i) │ │
└──────┬──────┘ └──────┬──────┘ │
│ │ │
▼ ▼ │
┌─────────────┐ ┌─────────────┐ │
│ next iter │◄───────────────│ next iter │◄─┘
└─────────────┘ └─────────────┘Using continue with while Loops
import random
attempts = 0
while attempts < 10:
attempts += 1
value = random.randint(1, 10)
if value < 5:
continue # Try again
print(f"Got valid value: {value}")
breakOutput:
Got valid value: 5Combining pass and continue
Sometimes you need both in different contexts:
class DataProcessor:
def validate(self, item):
pass # Subclass will implement
def process_all(self, items):
for item in items:
if not self.validate(item):
continue # Skip invalid items
self.handle(item)When to Use Each
| Scenario | Use |
|---|---|
| Empty function/class definition | pass |
| Placeholder for future code | pass |
| Empty except block (intentionally ignore) | pass |
| Skip current loop iteration | continue |
| Filter out items while iterating | continue |
| Avoid deeply nested conditions | continue |
Summary
- Use
passwhen you need syntactically valid code but have no logic to execute yet (empty functions, classes, or intentionally empty blocks) - Use
continuewhen you want to skip the rest of the current loop iteration and move to the next one (filtering, validation, early exit from iteration)