Unit 3 Β· Lesson 5

break, continue & Loop-Else

Sometimes you need to bail out of a loop early, or skip certain iterations. Python gives you three precise tools for fine-grained loop control.

Use break to exit a loop early
Use continue to skip iterations
Understand the for...else pattern
Choose the right tool for each situation

Three Loop Control Tools

πŸ›‘ break

Immediately exits the entire loop. Code after the loop runs next.

for i in range(10):
  if i == 5:
    break
# jumps here

⏭️ continue

Skips the rest of this iteration. Jumps back to the loop condition.

for i in range(5):
  if i == 2:
    continue
  print(i)
# 0,1,3,4

πŸ”„ for...else

The else block runs only if the loop finished without hitting break.

for i in data:
  if found:
    break
else:
  print("not found")

break β€” The Emergency Exit

break is perfect when you're searching for something and want to stop as soon as you find it:

# Find the first even number numbers = [7, 3, 11, 8, 5, 2] for n in numbers: if n % 2 == 0: print(f"Found it: {n}") break # Output: Found it: 8

continue β€” Skip and Move On

continue skips the rest of the current iteration and jumps to the next one:

# Print only positive numbers for x in [3, -1, 4, -5, 9]: if x < 0: continue # skip negatives print(x) # Output: 3, 4, 9

for...else β€” "Did We Find It?"

Python's unique for...else pattern: the else block runs only if the loop completed without break. Perfect for search-and-report:

for i in range(2, num): if num % i == 0: print(f"{num} is NOT prime (divisible by {i})") break else: print(f"{num} IS prime!")
How for...else works: If the loop runs to completion (range exhausted, no break hit) β†’ the else block runs. If break fires β†’ the else block is skipped. Think of it as "if the loop didn't break."
Practice Time

Challenges

πŸ”€

Challenge 1: First Vowel Finder

Guided

Find the first vowel in a word using break.

Instructions: Loop through each character in word. When you find a vowel (a, e, i, o, u β€” check lowercase), store it in first_vowel and break. If no vowel is found, first_vowel should stay "none".

Hint: Use for char in word: to iterate through characters.
Hint 1: for char in word: gives you each letter.
Hint 2: if char.lower() in "aeiou": checks if it's a vowel.
Hint 3: Inside the if: first_vowel = char then break
⏭️

Challenge 2: Positive Sum

Guided

Sum only the positive numbers from a list, using continue to skip negatives.

Instructions: Loop through numbers. Use continue to skip any negative values. Add positive values to total.
Hint 1: for n in numbers:
Hint 2: if n < 0: continue β€” skips to the next iteration
Hint 3: After the continue check: total += n
πŸ”

Challenge 3: Password Attempts

Solo

Simulate a login system with limited attempts using a loop with break.

Instructions: The correct password is secret. Loop through the attempts list. If a guess matches, set result to "Access granted" and break. If none match, result should be "Account locked".

Try using for...else for this!
Hint 1: for guess in attempts: β€” try each one
Hint 2: if guess == secret: result = "Access granted" then break
Hint 3: Use for...else: the else: block sets result = "Account locked" β€” it only runs if no break happened.
πŸ”

Challenge 4: Prime Finder

Stretch

Find all prime numbers up to limit using nested loops with for...else.

Instructions: For each number from 2 to limit, use an inner loop to check if it's prime. If no divisor is found (loop completes without break), it's prime β€” add it to the primes string. Separate primes with spaces.

Example: limit=20 β†’ "2 3 5 7 11 13 17 19"
Hint 1: Outer: for num in range(2, limit + 1):
Hint 2: Inner: for i in range(2, num): β€” if num % i == 0: break
Hint 3: Add else: after the inner loop (aligned with inner for): primes += str(num) + " "