Loop Controls (break & continue)
Sometimes when running a loop, you need to change its normal execution flow on the fly:
- Stop early: Exit the loop immediately once you find what you need.
- Skip a step: Skip only one specific item and move directly to the next.
To control loops dynamically, Python provides two special keywords: break and continue.
Normal Loop Flow vs. Loop Controls
Normally, a loop follows this standard step-by-step flow:
- Get the next item from the sequence.
- Execute the block of code.
- Move to the next item.
- Repeat until the loop finishes.
break and continue change this normal flow:
breakchanges the flow by exiting the loop completely.continuechanges the flow by skipping only the current iteration and moving to the next one.
1. The break Statement (Stop Immediately)
break stops the current loop immediately. Python exits the loop and does not execute any remaining iterations.
Visual Behavior of break (Instant Exit)
Real-Life Analogy: Searching for Lost Keys
Imagine you are searching for your house keys across 5 pockets in your bag:
- Check Pocket 1 → Not there.
- Check Pocket 2 → Found your keys!
- Do you check Pockets 3, 4, and 5? NO! You stop searching immediately because your goal is achieved.
Code Example: Searching for a Number
for num in range(1, 11):
if num == 7:
print("Found 7! Stopping search.")
break
print(f"Checking number: {num}")
Output:
Checking number: 1
Checking number: 2
Checking number: 3
Checking number: 4
Checking number: 5
Checking number: 6
Found 7! Stopping search.
Python completely stopped searching and never checked numbers 8, 9, or 10!
2. The continue Statement (Skip One Round)
continue does not stop the loop. It only skips the remaining code for the current item and moves directly to the next iteration.
Key Difference to Remember:
break: Stops the entire loop completely.continue: Skips only the current round.
Visual Behavior of continue (Skip & Move to Next)
Real-Life Analogy: Skipping Bad Apples
Imagine you are packing apples into a box:
- Apple 1 is good → Pack it.
- Apple 2 is good → Pack it.
- Apple 3 is spoiled → Skip it! Move directly to Apple 4.
- Apple 4 is good → Pack it.
Code Example: Skipping Unlucky Number 3
for num in range(1, 6):
if num == 3:
print("Skipping unlucky number 3!")
continue
print(f"Processing number: {num}")
Output:
Processing number: 1
Processing number: 2
Skipping unlucky number 3!
Processing number: 4
Processing number: 5
Notice that Python skipped number 3, but did not stop the loop! It moved straight to 4 and 5.
3. Quick Note: pass Statement (Temporary Placeholder)
Unlike break and continue, pass does not change loop execution. It simply acts as an empty placeholder where code will be added later.
What if you want to write an if statement or a loop structure now, but add the code inside it later?
In Python, an empty block causes a syntax error. To leave code blank without crashing your program, type pass as a placeholder:
for num in range(1, 4):
if num == 2:
# TODO: Add code here later! For now, do nothing:
pass
print(f"Number: {num}")
pass simply tells Python: "Do nothing for now and keep going."
Quick Summary
| Keyword | Behavior | Flow Impact |
|---|---|---|
| Normal Loop | Processes each item in sequence step by step. | Item → Execute block → Next item |
break | Stops the entire loop immediately and exits. | Item → Stop loop completely |
continue | Skips only the current iteration and moves to the next. | Item → Skip current work → Next item |
pass | Does nothing and acts as a placeholder for future code. | No change to execution flow. |
What's Next?
Now that you can control single loops, what if you need to run a loop inside another loop? Let's explore nested loops in the next lesson!