Python Loops
Estimated reading: 3 minutes 264 views

Python break, continue, and pass – Loop Control Made Easy

Introduction – Why Loop Control Statements Matter

Loops are fundamental in Python, but sometimes you need more than just basic iteration. You may want to exit a loop early, skip specific iterations, or insert a placeholder for code you’ll add later.

This is where Python’s loop control statements come in:

  • break – exit a loop immediately
  • continue – skip to the next iteration
  • pass – do nothing, just a placeholder

Understanding how to use these enhances control, clarity, and power in your Python code.


break Statement – Stop the Loop Instantly

The break statement is used to exit the nearest enclosing loopfor or while—before it finishes naturally.

Syntax:

for item in iterable:
    if condition:
        break

Example:

for num in range(1, 10):
    if num == 5:
        break
    print(num)

Output:

1
2
3
4

Explanation:
The loop breaks when num == 5. Everything after that is skipped.


continue Statement – Skip This Iteration

The continue statement tells Python to skip the rest of the loop body and go to the next iteration.

Syntax:

for item in iterable:
    if condition:
        continue

Example:

for num in range(1, 6):
    if num == 3:
        continue
    print(num)

Output:

1
2
4
5

Explanation:
When num == 3, it skips printing and moves to the next number.


pass Statement – Placeholder for Future Code

The pass statement does nothing. It’s used as a placeholder when a statement is syntactically required but no action is needed yet.

Syntax:

for item in iterable:
    pass

Example:

for char in "Python":
    pass

Use Case:
Use pass to temporarily skip implementation of a loop, function, or class.


Real-World Example: Login Retry System

password = "admin123"
attempts = 0

while attempts < 3:
    entry = input("Enter password: ")
    if entry == password:
        print("Access granted.")
        break
    else:
        print("Incorrect password.")
        attempts += 1
else:
    print("Too many attempts. Access denied.")

Uses:

  • break to exit the loop on correct password
  • else to handle repeated failed attempts

Common Pitfalls

PitfallFix / Tip
Using break outside loopsbreak must be inside a loop
continue causes infinite loopUpdate your loop variable properly
pass used unintentionallyBe clear it’s just a no-op placeholder

Summary – Key Takeaways

  • break: Terminates the loop immediately.
  • continue: Skips current iteration and continues looping.
  • pass: A null operation; does nothing but keeps syntax valid.
  • Use these statements to improve clarity, flow control, and debugging.

FAQ Section

Can I use break in both for and while loops?

Yes, break works in both.

Does continue skip the else block of a loop?

No. Only break prevents the else from executing.

When should I use pass?

Use pass when you want to write code later, or in stub functions, classes, or loops.

Can I use break and continue in nested loops?

Yes, but they only affect the nearest enclosing loop.

Does pass affect performance?

No. It simply does nothing—it’s ignored during execution.


Share Now :
Share

Python break / continue / pass

Or Copy Link

CONTENTS
Scroll to Top