Loop Control Statements
Overview
Learn to modify loop behavior using break, continue, and pass. These tools help you fine-tune how and when a loop should stop, skip steps, or do nothing temporarily.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- What break does: Understand how to exit a loop early when a condition is met.
- What continue does: Learn how to skip the rest of the current iteration and move to the next one.
- What pass does: Discover how to use a placeholder statement that does nothing.
- When to use each: Recognize situations where each control statement is most useful.
Why This Matters
Sometimes you need more control over loops than just letting them run normally. You might want to stop early when you find what you're looking for (break), skip certain iterations (continue), or temporarily leave code empty (pass). These statements give you that flexibility.
Step 1: The break Statement
The break statement immediately exits the loop, even if the loop condition is still True. Think of it like an emergency exit - when you hit break, Python stops the loop completely and continues with the code after the loop.
for i in range(10):
print(i)
if i == 5:
break
print("Loop finished")
Normal Loop Execution
The loop starts normally, processing each item or iteration as usual.
Condition Check
Inside the loop, you check if a condition is met (like finding what you're looking for).
Execute break
If the condition is True, you execute break, which immediately exits the loop.
Continue After Loop
Python continues with the code after the loop, skipping any remaining iterations.
Key Point: When Python encounters break, it doesn't finish the current iteration. It immediately stops the entire loop and jumps to the code after the loop. This is different from letting the loop finish naturally.
Mini Practice #1: Using break
Try It YourselfTry this example that uses break to stop early:
What happened? The loop started processing numbers from 1 to 9. It printed "Checking 1...", then "Checking 2...", and so on. When it reached 5, it printed "Checking 5..." and then "Found it!". At that point, break was executed, which immediately stopped the loop. Python didn't continue to check 6, 7, 8, or 9 - it jumped straight to the code after the loop and printed "Done!". This is useful when you're searching for something and want to stop as soon as you find it!
Step 2: The continue Statement
The continue statement skips the rest of the current iteration and moves to the next one. Think of it like skipping to the next song in a playlist - you don't stop the playlist, you just skip the current song and move forward.
for i in range(10):
if i % 2 == 0: # Skip even numbers
continue
print(i)
Loop Starts Iteration
The loop begins processing the current item or iteration.
Condition Check
You check if you want to skip this iteration (like checking if a number is even).
Execute continue
If the condition is True, you execute continue, which skips the rest of the current iteration.
Move to Next Iteration
Python immediately moves to the next iteration of the loop, continuing the loop normally.
Key Difference: Unlike break, which stops the entire loop, continue just skips the current iteration and keeps the loop running. The loop continues with the next item in the sequence.
Mini Practice #2: Using continue
Try It YourselfTry this example that uses continue to skip even numbers:
What happened? The loop went through numbers 1 to 10. For each number, it checked if it was even using i % 2 == 0 (this checks if the remainder when dividing by 2 is 0). When the number was even (2, 4, 6, 8, 10), continue was executed, which skipped the print statement and moved to the next number. When the number was odd (1, 3, 5, 7, 9), the loop continued normally and printed "Odd number: [number]". Notice how the loop didn't stop - it just skipped certain iterations and kept going!
Step 3: The pass Statement
The pass statement is a placeholder that does nothing. It's useful when you need to write code syntactically (because Python requires something) but you don't want to do anything yet. Think of it like a placeholder card that says "to be filled in later".
for i in range(5):
if i == 2:
pass # TODO: Add logic here later
else:
print(i)
When to Use pass
You use pass when:
- You're writing code structure but haven't implemented the logic yet
- You need something syntactically but don't want it to do anything
- You're planning to add code later and need a placeholder
Unlike break and continue, pass doesn't affect loop flow - it just does nothing and continues normally.
Step 4: Comparing break, continue, and pass
Understanding the difference helps you choose the right one:
break
Stops the entire loop immediately.
for i in range(10):
if i == 5:
break
print(i)
Prints: 0, 1, 2, 3, 4
Loop stops completely
continue
Skips current iteration, continues loop.
for i in range(10):
if i == 5:
continue
print(i)
Prints: 0, 1, 2, 3, 4, 6, 7, 8, 9
Skips 5, keeps going
pass
Does nothing, continues normally.
for i in range(10):
if i == 5:
pass
print(i)
Prints: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
All numbers print
Memory Aid: Think of break as "break out of the loop", continue as "continue to the next iteration", and pass as "pass it by" (do nothing).
End-of-Lesson Exercises
Exercise 1: Use break
Write a for loop that goes through range(1, 11). Print each number, but use break to stop when you reach 7.
Use for i in range(1, 11):, print i, then check if i == 7 and break.
Exercise 2: Use continue
Write a for loop that goes through range(1, 11). Print each number, but use continue to skip numbers divisible by 3.
Use for i in range(1, 11):, check if i % 3 == 0 and continue, then print i.