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 │◄─┘
└─────────────┘ └─────────────┘