Loop Controls (break, continue & pass)
Sometimes when you are running a loop, you need to change its normal flow on the fly:
- Stop early: Stop the loop immediately once you find what you need.
- Skip a step: Skip only one specific round and move to the next.
- Do nothing: Leave a placeholder to write code later.
To control loops dynamically, Python provides three special keywords: break, continue, and pass.
1. The break Statement (Stop Immediately)
The break statement instantly stops the loop and exits it. Python ignores all remaining rounds of the loop.
Real-Time Analogy: Leg Cramp
Imagine you plan to run 10 laps around a track. But on lap 5, you get a leg cramp. You must stop running completely and leave the track.
Code Example:
# Count from 1 to 10, but stop early if we reach 5
for i in range(1, 11):
if i == 5:
print("Reached 5! Stopping the loop early.")
break
print(f"Runner completed lap: {i}")
Output:
Runner completed lap: 1
Runner completed lap: 2
Runner completed lap: 3
Runner completed lap: 4
Reached 5! Stopping the loop early.
Notice that Python completely stopped running the loop and never reached lap 6, 7, 8, 9, or 10!
2. The continue Statement (Skip One Round)
The continue statement stops only the current round of the loop. It immediately jumps back to the top of the loop to start the next round.
Real-Time Analogy: Skipping a Step
Imagine you are walking up 5 flights of stairs, but step number 3 is wet. You skip step 3 and step directly onto step 4.
Code Example:
# Count from 1 to 5, but skip number 3
for i in range(1, 6):
if i == 3:
print("Step 3 is wet! Skipping it...")
continue
print(f"Climbed step: {i}")
Output:
Climbed step: 1
Climbed step: 2
Step 3 is wet! Skipping it...
Climbed step: 4
Climbed step: 5
Notice that Python did not print "Climbed step: 3", but it did not stop the loop. It just skipped that one round and continued with step 4!
3. The pass Statement (Do Nothing / Placeholder)
The pass statement is a silent helper. It does absolutely nothing.
In Python, you cannot leave a loop or an if block empty (doing so raises an error). If you want to create a block of code but write the instructions inside it later, you type pass as a placeholder.
Code Example:
# Count from 1 to 5, but do nothing on number 3
for i in range(1, 6):
if i == 3:
# We will add some code here later! For now, do nothing:
pass
print(f"Number is: {i}")
Output:
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Number is: 5
Notice that pass did not stop the loop (like break), and did not skip the print statement (like continue). It simply did nothing and let the code run normally.
Hands-On Lab Exercises
Practice writing loops by running these scripts on your computer:
# Exercise 1: Guess the Password (Max 3 attempts)
attempts = 0
while attempts < 3:
password_input = input("Enter password: ")
if password_input == "ThinkIT":
print("Success! Access Granted.")
break
attempts = attempts + 1
print(f"Wrong password. Attempts remaining: {3 - attempts}")
else:
print("Locked out! You used all 3 attempts.")
# Exercise 2: Print numbers 1 to 10 but skip number 5
for i in range(1, 11):
if i == 5:
print("Found 5! Skipping it...")
continue
print(i)
Simple Q&A
Q1: When should I use pass instead of continue?
Answer: Use continue when you want to skip the remaining lines of the current round and start the next round immediately. Use pass when you just need a temporary empty block to prevent Python from crashing with a syntax error.
Q2: What does the else block do on a loop?
Answer: An else block attached to a loop only runs if the loop finishes all its rounds naturally without hitting a break statement. For example, in Exercise 1, if the user enters the wrong password 3 times, the loop finishes naturally and the else block runs to lock the user out.
Module Summary
forLoops: Used to repeat code over a set sequence (like letters in a word or a range of numbers).range(): Generates a sequence of numbers (stops 1 step early!).whileLoops: Repeats code while a condition isTrue. Make sure to update your condition to avoid infinite loops!break: Exits the loop immediately.continue: Skips the current round and jumps to the next.pass: A silent placeholder that does nothing.