What is the Difference Between continue and pass in Python?
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:
# 1. Defining an empty class
class CustomError(Exception):
pass
# 2. Creating a function stub (TODO)
def complex_algorithm():
pass # TODO: Implement this later
# 3. Empty conditional branch
def process(value):
if value < 0:
pass # Negative values need no action
else:
print(f"Processing {value}")
# 4. Ignoring specific exceptions
try:
result = risky_operation()
except KeyboardInterrupt:
pass # Silently ignore Ctrl+C
pass is purely syntactic. It tells Python "there's intentionally no code here" without affecting program flow. Execution continues to the next line as normal.
Using 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
# This code is UNREACHABLE for "Admin"
print(f"Sending email to {user}")
Output:
Sending email to Alice
Sending email to Bob
Skipping admin account...
Sending email to Charlie
Common Use Cases for continue
# Filtering invalid data
for row in data:
if not row.is_valid():
continue
process(row)
# Skipping comments in file processing
for line in file:
if line.startswith('#'):
continue
parse(line)
# Avoiding nested conditions
for item in items:
if item is None:
continue
if item.status != 'active':
continue
# Main logic here (less indentation)
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
2
With continue:
for i in range(3):
if i == 1:
continue # Skips to next iteration
print(i)
Output:
0
1
2
With pass, the number 1 is printed because execution continues to print(i). With continue, the print(i) line is never reached for i == 1.
Visual 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}")
break
Output:
Got valid value: 5
Be careful with continue in while loops. Ensure the loop condition variable is updated before continue, or you may create an infinite loop:
# ⛔️ WRONG: Infinite loop
i = 0
while i < 5:
if i == 2:
continue # i never increments past 2
print(i)
i += 1
# ✅ CORRECT: Increment before continue
i = 0
while i < 5:
if i == 2:
i += 1
continue
print(i)
i += 1
Output:
0
1
Combining 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)
When reviewing code, pass signals "intentionally empty" while continue signals "skip this case." Using them correctly communicates your intent clearly to other developers.