break — the emergency exit
The problem...
Your loop is running. It finds what it was looking for. But it keeps going anyway — checking every item that's left, doing work that doesn't need to be done.
There's no reason to continue. But the loop doesn't know that.
The idea!
break stops the loop immediately. The moment it executes — the loop is done. No more iterations. No more checks. Out.
The syntax
for item in sequence:
if condition:
break # stop the loop immediately
Your first break
word = "RedHorn"
for letter in word:
if letter == "H":
print("Found H — stopping.")
break
print(letter)
# R
# e
# d
# Found H — stopping.
The loop hits "H" — prints the message — breaks. "o", "r", "n" are never touched.
continue vs break
word = "Bull"
# continue — skips one iteration, loop keeps going
for letter in word:
if letter == "u":
continue
print(letter)
# B / l / l
# break — stops the loop entirely
for letter in word:
if letter == "u":
break
print(letter)
# B
continue is polite — it skips and moves on. break is final — it stops everything.
A real example
word = input("Enter a word: ")
found = False
for letter in word:
if letter.lower() in "aeiou":
print(f"First vowel found: {letter}")
found = True
break
if not found:
print("No vowels found.")
The loop searches for the first vowel. The moment it finds one — it stops. No need to check the rest.
Heads up!
breakstops the entire loop — not just the current iteration- Everything after
breakin the loop is never reached breakalways works with a condition — alone it would stop on the first iterationcontinueskips one iteration —breakends the loop
The mindset shift
Stop thinking: "The loop has to finish."
Start thinking: "The loop stops when the job is done."
What you should understand now
breakstops the loop immediately — no more iterations- Use it when you've found what you need and there's no reason to continue
continueskips one —breakstops all- Everything after
breakin the loop block is ignored