Quest 13: Iterative Loops - The Journey vs The Destination
Understanding For Loops vs While Loops
๐ QUEST 13 | Difficulty: Intermediate | Time: 5 minutes
๐ Complexity Level: Intermediate โญโญ
Builds on fundamental concepts from earlier quests. Best for students who have completed Quests 1-6 or have some basic programming experience. This deepens your understanding of loops.
๐ป Interactive Options:
- ๐ Open in JupyterLite - Full Jupyter environment in your browser
- โถ๏ธ Run code directly below - All code cells on this page are editable and runnable
- ๐ฅ Download Notebook - For use in local Jupyter or Google Colab
๐ Introduction: Two Paths Up the Mountain
Imagine youโre climbing a mountain. You can take two approaches:
Path 1 (The Map): You know there are 10 checkpoints. You hike to each one in order: checkpoint 1, 2, 3โฆ checkpoint 10. Done. (For Loop)
Path 2 (The Rule): You hike until the sky clears (checking every step). Once you see clear skies, you stop. (While Loop)
Both get you up the mountain, but: - For loops: You know in advance how many iterations - While loops: You iterate until a condition becomes false
๐๏ธ Story Time: Youโre leading adventurers up a treacherous mountain. Some paths have markers at each section (for loops). Others just have a rule: โKeep climbing until the path is clearโ (while loops). A skilled adventurer knows which tool suits the situation!
๐ก Comparison: For vs While
For Loop: When You KNOW The Count
# Example: Exactly 5 doors to check
for i in range(5):
print(f"Checking door {i}")Use for when: - โ
Iterating through a known collection (list, string, range) - โ
You know exactly how many iterations you need - โ
Processing each item in a sequence
While Loop: When You DONโT Know When to Stop
# Example: Keep searching until you find the magic door
magic_door_found = False
door_number = 0
while not magic_door_found:
print(f"Checking door {door_number}")
if door_number == 42:
magic_door_found = True
door_number += 1Use while when: - โ
You donโt know how many iterations in advance - โ
Youโre responding to a condition being true/false - โ
You need to ask โshould I continue?โ at each step
๐ฏ The Key Difference
| Feature | For Loop | While Loop |
|---|---|---|
| When to use | Count known | Count unknown |
| Iteration count | Set in advance | Determined by condition |
| Best for | Collections, sequences | Conditions, searches |
| Danger | None (bounds are fixed) | Infinite loops if condition never becomes false! |
๐ฎ Activity: Search Patterns
Letโs see both approaches solving the same problem:
๐งฉ Challenge: The Treasure Chest
Write code that: 1. Uses a while loop to generate random treasure values between 1-100 2. Stops when you find a piece of treasure worth 80 or more 3. Count how many treasures you had to check before finding a valuable one
โ Solution
๐ Bonus: When to Use break and continue
Two special loop control keywords:
๐ What You Learned
โจ For loops are for counting through sequences โจ While loops are for repeating until a condition changes โจ break exits a loop immediately โจ continue skips to the next iteration โจ Both can solve the same problemโpick based on your knowledge!
๐ Whatโs Next?
Ready to write smarter code? Check out Quest 14: Functions Part 2 - Non-Returning Functions to learn about functions that do things without returning values!