Python · Loops
Loop Control
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Loop Control Introduction
Sometimes we need more control over our loops. Python provides two special statements for this purpose:
break
and
continue
.
These statements let you change the normal flow of a loop: -
break
: Exits the loop completely -
continue
: Skips the current iteration and moves to the next one
Loop control statements help you make your loops more efficient and solve problems in different ways.
Exercise 2 — The break Statement
The
break
statement allows you to exit a loop immediately, regardless of the loop condition. Once
break
is executed, the program continues with the next statement after the loop.
codemons = ["Aquachu", "Florachu", "Pyrochu"] for codemon in codemons: print("Checking", codemon) if codemon == "Florachu": print("Found Florachu!") break # Exit the loop when Florachu is found print("Search complete.")
In this example, we're looking for "Florachu" in our list. Once we find it, we print a message and then use
break
to exit the loop immediately. Notice that "Pyrochu" is never checked because we already exited the loop.
When to use break
Use break when you: - Found what you were looking for - Need to exit a loop early based on a condition - Want to implement an "early exit" strategy for efficiency
Exercise 3 — The continue Statement
The
continue
statement skips the rest of the current iteration and moves directly to the next iteration of the loop. Unlike
break
, it doesn't exit the loop completely.
codemons = ["Aquachu", "Florachu", "Pyrochu"] for codemon in codemons: # Skip grass types if codemon == "Florachu": continue # Skip to the next codemon print(codemon, "is ready for battle!") print("Battle preparation complete.")
In this example, we use
continue
to skip Florachu. When the condition is true, the
continue
statement causes the loop to jump immediately to the next iteration without executing the print statement.
When to use continue
Use continue when you: - Want to skip certain items in a collection - Need to avoid processing specific cases - Want to implement filtering logic within a loop
Exercise 4 — What does break do?
What will be the output of this code?
for i in range(1, 6): if i == 3: break print(i) print("Done")
Options
• 1 2 Done
• 1 2 3 Done
• 1 2 4 5 Done
• 1 2 3 4 5 Done
Hint : Remember: When a break statement is executed, Python immediately exits the loop and continues with the next statement after the loop.
Exercise 5 — What does continue do?
What will be the output of this code?
for i in range(1, 6): if i == 3: continue print(i) print("Done")
Options
• 1 2 4 5 Done
• 1 2 Done
• 1 2 3 4 5 Done
• 1 2 3 Done
Hint : Remember: continue skips the rest of the current iteration and moves to the next item in the loop. It doesn't exit the loop completely.
Exercise 6 — Break vs Continue
Match each scenario with the appropriate loop control statement.
Items to match
• Exit the loop when a specific value is found
• Skip processing negative numbers
• Stop counting when reaching a threshold
• Only process even numbers in a list
Definitions
• break
• continue
• break
• continue
Exercise 7 — Break or Continue?
Complete the code to find the first even number and then stop the search.
numbers = [7, 3, 9, 4, 1, 8] for num in numbers: if num % 2 == 0: print("First even number found:", num) ____ print("Search complete.")
Options: break continue pass exit return
Hint : Break is used to exit the loop completely when a condition is met, Continue is used to skip the rest of the current iteration and move to the next one.
Exercise 8 — Break or Continue?
Complete the code to skip negative numbers and only print positive numbers.
numbers = [5, -2, 7, -1, 0, 3, -4] for num in numbers: if num < 0: ____ print(num, "is a non-negative number.")
Options: continue break pass skip next
Hint : Break is used to exit the loop completely when a condition is met, Continue is used to skip the rest of the current iteration and move to the next one.
Exercise 9 — Loop Controls in While Loops
The
break
and
continue
statements work exactly the same way in while loops as they do in for loops.
Using
break
.
codemon_health = 100 turn = 1 while codemon_health > 0: print("Turn", turn, "- Health:", codemon_health) damage = turn * 10 codemon_health -= damage # End the battle early if health drops below 50 if codemon_health < 50: print("Codémon is at risk! Exiting battle.") break turn += 1 print("Battle ended.")
Using
continue
.
count = 0 while count < 10: count += 1 # Skip even numbers if count % 2 == 0: continue print("Processing odd number:", count) print("Processing complete.")
Be Careful with continue in While Loops
When using continue in a while loop, make sure your loop variable still gets updated. Otherwise, you might create an infinite loop if the update statement is placed after the continue.
Exercise 10 — What will this code print?
What will be the output of this while loop with break?
i = 1 while i <= 5: if i == 3: i += 1 break print(i) i += 1 print("Done")
Options
• 1 2 Done
• 1 2 3 Done
• 1 2 4 5 Done
• 1 2 3 4 5 Done
Hint : Check what happens in the case where i equals 3. Where does the execution flow? What statements are executed and in what order?
Exercise 11 — Codémon Challenge - Inventory Manager
Welcome to the Inventory Manager! You need to process items in your Codémon inventory using loop control statements. Your tasks: 1. Loop through the items list and stop searching when you find a "Rare Candy". Display each item you check and a special message when you find the Rare Candy. 2. Loop through the power_levels list and only display power levels that are 50 or higher. Skip (don't display) power levels below 50.
Starter code
print("=== INVENTORY MANAGER ===") # Task 1: Find Rare Candy and stop print("\n--- Searching for Rare Candy ---") items = ["Potion", "codeball", "Rare Candy", "Revive", "Super Potion"] # Task 2: Display only high power levels (50+) print("\n--- High Power Levels ---") power_levels = [35, 62, 48, 55, 73, 42, 68]
Back to the course