Python Loops
Estimated reading: 3 minutes 34 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 loopβ€”for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python break / continue / pass

Or Copy Link

CONTENTS
Scroll to Top