Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. pass / break / continue

pass / break / continue

Python's pass, break, and continue are keywords that control the flow of loops and conditional statements. pass is an explicit no-op (empty statement) that can be used as a placeholder wherever a block is syntactically required. break immediately terminates the current loop, and continue skips the remaining code in the current iteration and moves to the next one.

Syntax

# pass: no-op. Used as a placeholder where a block is syntactically required.
if condition:
    pass   # placeholder for future implementation

# Also used for stub functions and classes.
def function_name():
    pass

class ClassName:
    pass

# break: immediately exits the current loop.
for variable in iterable:
    if condition:
        break   # exits the loop; the else clause is skipped

# continue: skips the remaining code and moves to the next iteration.
for variable in iterable:
    if condition:
        continue   # skips remaining code, starts next iteration

# break in nested loops: exits only the inner loop.
for variable_a in iterable_a:
    for variable_b in iterable_b:
        if condition:
            break   # exits only the inner loop; outer loop continues

# Combining with else: the else block runs if the loop was NOT broken.
for variable in iterable:
    if condition:
        break
else:
    # runs only if the loop ran to completion (no break occurred)
    pass

Keywords

KeywordNotes
passNo-op empty statement. Used as a placeholder where a block is syntactically required.
breakImmediately exits the current loop. Works with both for and while. The else clause is skipped.
continueSkips the remaining code in the current iteration and starts the next iteration.
for ... else: / while ... else:Runs the else block when the loop completes without a break.

Sample Code

Using pass to create empty blocks for stub functions, classes, and conditions. This sample shows stubs modeled on Steins;Gate characters.

pass_placeholder.py
# Use pass to stub out functions for later implementation.
def send_d_mail(message, target_time):
    pass   # TODO: implement time machine mail sending

def revert_world_line():
    pass   # TODO: implement worldline divergence reset

# Use pass to stub out a class for later implementation.
class PhoneWave:
    pass   # TODO: implement Phone Microwave (Name Subject to Change)

# Use pass when you intentionally want to do nothing in a branch.
lab_members = ["Okabe Rintaro", "Shiina Mayuri", "Hashida Itaru", "Makise Kurisu"]
target = "Faris"

if target in lab_members:
    print(target, "is a lab member")
else:
    pass   # Do nothing if not a lab member.

print("Stub verification complete")
python3 pass_placeholder.py
Stub verification complete

Using break to exit a loop immediately. This sample stops a search as soon as a matching time-leap memory is found.

break_example.py
# Search a time-leap history list for the target worldline.
leaps = [
    ("1048596", "Beta worldline"),
    ("1130205", "Gamma worldline"),
    ("0571046", "Alpha worldline"),
    ("1000000", "Steins;Gate"),
]
target_attractor = "Steins;Gate"

# break: exits the loop immediately when the target is found.
for divergence, name in leaps:
    if name == target_attractor:
        print(f"Target convergence point found: {name} ({divergence}%)")
        break
    print(f"{name} ({divergence}%) is not the target")
else:
    # Runs when break was not triggered (target not found).
    print(target_attractor, "was not found")
python3 break_example.py
Beta worldline (1048596%) is not the target
Gamma worldline (1130205%) is not the target
Alpha worldline (0571046%) is not the target
Target convergence point found: Steins;Gate (1000000%)

Using continue to skip specific iterations. This sample processes only lab members who meet a certain condition.

continue_example.py
# List of lab members.
lab_members = [
    ("Okabe Rintaro", True),    # (name, is official lab member)
    ("Shiina Mayuri", True),
    ("Hashida Itaru", True),
    ("Makise Kurisu", True),
    ("Kiryu Moeka", False),     # Not an official member.
    ("Urushibara Ruka", True),
    ("Amane Suzuha", True),
    ("Faris", False),           # Not an official member.
]

print("=== Official Lab Members ===")
for name, is_member in lab_members:
    # Skip to the next member if not an official member.
    if not is_member:
        continue
    print("Lab member:", name)
python3 continue_example.py
=== Official Lab Members ===
Lab member: Okabe Rintaro
Lab member: Shiina Mayuri
Lab member: Hashida Itaru
Lab member: Makise Kurisu
Lab member: Urushibara Ruka
Lab member: Amane Suzuha

Using break in nested loops. Only the inner loop exits; the outer loop continues. This sample searches a D-Mail log for a specific combination.

nested_break.py
# Search send dates and recipients for a specific D-Mail.
send_dates = ["Jul 28", "Jul 29", "Aug 3"]
recipients = ["Shiina Mayuri", "Makise Kurisu", "Amane Suzuha"]
target_recipient = "Makise Kurisu"

for date in send_dates:
    print(f"--- Checking {date} send log ---")
    for recipient in recipients:
        if recipient == target_recipient:
            print(f"  D-Mail to {recipient} on {date} found")
            break   # Exits only the inner loop. Outer loop continues.
        print(f"  {recipient}: not a match")
python3 nested_break.py
--- Checking Jul 28 send log ---
  Shiina Mayuri: not a match
  D-Mail to Makise Kurisu on Jul 28 found
--- Checking Jul 29 send log ---
  Shiina Mayuri: not a match
  D-Mail to Makise Kurisu on Jul 29 found
--- Checking Aug 3 send log ---
  Shiina Mayuri: not a match
  D-Mail to Makise Kurisu on Aug 3 found

Combining break with else. The else block runs only if the loop completed without a break. This sample determines whether a specific worldline was reached.

break_else.py
# List of tried worldline divergence values.
tried_divergences = ["0.000000", "0.337187", "0.571046", "1.048596"]
goal = "1.000000"   # Steins;Gate worldline divergence.

for divergence in tried_divergences:
    if divergence == goal:
        print(f"Reached Steins;Gate: {divergence}%")
        break   # else clause is skipped when break runs.
else:
    # Runs when break was not triggered (target not found in all elements).
    print(f"Could not reach Steins;Gate ({goal}%)")
    print("Continuing to time-leap...")
python3 break_else.py
Could not reach Steins;Gate (1.000000%)
Continuing to time-leap...

Common Mistakes

Common Mistake 1: break exits only the inner loop in nested loops

break exits only the innermost loop it is in. The outer loop continues. To exit all loops, use a flag variable or extract the loop body into a function and use return.

ng_nested_break.py
send_dates = ["Jul 28", "Jul 29", "Aug 3"]
recipients = ["Shiina Mayuri", "Makise Kurisu", "Amane Suzuha"]
target = "Makise Kurisu"

# Wrong: we want to stop at the first match, but the outer loop continues.
for date in send_dates:
    for recipient in recipients:
        if recipient == target:
            print("Found:", date, recipient)
            break   # Only exits the inner loop; outer loop runs 3 times.
python3 ng_nested_break.py
Found: Jul 28 Makise Kurisu
Found: Jul 29 Makise Kurisu
Found: Aug 3 Makise Kurisu
ok_nested_break.py
send_dates = ["Jul 28", "Jul 29", "Aug 3"]
recipients = ["Shiina Mayuri", "Makise Kurisu", "Amane Suzuha"]
target = "Makise Kurisu"

# Use a flag variable to exit the outer loop as well.
found = False
for date in send_dates:
    for recipient in recipients:
        if recipient == target:
            print("Found:", date, recipient)
            found = True
            break
    if found:
        break
python3 ok_nested_break.py
Found: Jul 28 Makise Kurisu

Common Mistake 2: Forgetting pass causes IndentationError

In Python, a block must contain at least one statement. Writing an empty block causes an IndentationError or SyntaxError. Use pass to represent an empty block.

ng_empty_block.py
# Wrong: empty blocks cause errors.
def send_d_mail():
    # TODO: implement later

class PhoneWave:
    # TODO: implement later
python3 ng_empty_block.py
  File "ng_empty_block.py", line 5
    class PhoneWave:
    ^
IndentationError: expected an indented block after function definition
ok_pass_placeholder.py
# Use pass as a stub.
def send_d_mail():
    pass   # TODO: implement later

class PhoneWave:
    pass   # TODO: implement later

print("Definitions complete")
python3 ok_pass_placeholder.py
Definitions complete

Common Mistake 3: Code after continue is not executed

When continue runs, all code written after it in the current iteration is skipped. Code placed after continue does not run when continue is triggered.

ng_continue_dead_code.py
lab_members = [
    ("Okabe Rintaro", True),
    ("Kiryu Moeka", False),
    ("Urushibara Ruka", True),
]

for name, is_member in lab_members:
    if not is_member:
        continue
        print(name, "is skipped")   # Wrong: this is after continue, so it never runs
    print("Lab member:", name)
python3 ng_continue_dead_code.py
Lab member: Okabe Rintaro
Lab member: Urushibara Ruka

To log non-members, write the log statement before continue.

ok_continue_order.py
lab_members = [
    ("Okabe Rintaro", True),
    ("Kiryu Moeka", False),
    ("Urushibara Ruka", True),
]

for name, is_member in lab_members:
    if not is_member:
        print(name, "skipped")   # Write before continue.
        continue
    print("Lab member:", name)
python3 ok_continue_order.py
Lab member: Okabe Rintaro
Kiryu Moeka skipped
Lab member: Urushibara Ruka

Notes

pass is a statement for explicitly expressing "do nothing". Because Python uses indentation to define blocks, an empty block cannot be written without it. Placing pass expresses "an intentionally empty block". A typical use is writing the skeleton of functions, classes, and conditional blocks early in development and deferring the implementation.

In nested loops, break exits only the innermost loop. Python has no dedicated syntax for exiting multiple loops at once. The common approaches are using a flag variable or extracting the loop body into a function and using return. Combining break with else lets you determine whether a search succeeded without an extra flag variable. For the details of else clauses on for and while, see for loop and while loop.

continue improves readability for cases where you want to skip elements that don't meet a condition. However, deeply nested code using continue can become hard to follow. Consider whether the same logic can be expressed with a list comprehension or filter() as an alternative.

If you find any errors or copyright issues, please .