Stop a search at a match
Quick review (2 minutes)
Someone looking for keys opens one drawer after another.
A break line ends the nearest loop around it at once, skips the rest of that pass and every later pass, and the program carries on with the first line after the loop. After a break, a for loop's name keeps its current item.
Above the loop, set any name that the program reads after the loop and that only a match changes, such as
found = False.Write the test that marks a match, and put it in an
ifinside the body.Inside that
if, do what the match needs, such as settingfound = True, and then writebreak.Trace the passes until the test is true. At the
break, jump to the first line after the loop.Check the case with no match. A for loop runs to its end, and a name that only a match changes, such as
found, still holds the value it was given above the loop. Awhile True:loop that reads input and meets no match keeps reading; when the input runs out,input()stops the program with an EOFError.
A message about a miss written in the body after the match test runs on every pass that misses: in a search for a space, once for each character before the space, or once for every character of a string with no space. Decide after the loop, with a name such as found, whether the search found nothing.
In a while True: loop, the test never ends the loop. If the test before the break is never true for the replies typed, as when each reply is compared with "Done" and the person types done each time, the loop keeps asking. It stops only with an error: an EOFError when the input runs out, or a KeyboardInterrupt when you press Control-C, the interrupt key.
1 In plain words
Someone looking for keys opens one drawer after another. When the keys turn up, they stop: the drawers further along are not opened.
A loop stops early with break. When Python runs a break line, it ends the loop at once, and the program carries on with the first line after the loop.
In this lesson's searches the break sits inside an if in the body, so it runs only when the test finds a match. With no match, a for loop runs to the end of its sequence. while True: makes a loop whose test is always true, so the test never ends it. A break does, and so does an error that stops the program, such as the EOFError of input() when the input runs out.
Remember one sentence: break leaves the loop now. To look it up, see section 4.4 of the Python 3.13 tutorial, on break and continue.
- Python Software Foundation, Python 3.13 Tutorial, 4.4 break and continue Statements
- Python Software Foundation, Python 3.13 Language Reference, 7.9 The break statement
- Python Software Foundation, Python 3.13 Language Reference, 8.1 The if statement
- Python Software Foundation, Python 3.13 Language Reference, 8.3 The for statement
- Python Software Foundation, Python 3.13 Language Reference, 8.2 The while statement
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: input
2 Picture it
A table of the passes of a loop that searches the string plan B for a space. On passes 1 to 4 the characters p, l, a and n do not match and are displayed. On pass 5 the space matches and break ends the loop. The character B is never reached. These are the passes of the first worked example. The row for the match is marked.
3 Why
break ends the loop that holds it, not only the if around it. Python skips the rest of the body and every pass that was still to come.
After a break in a for loop, the loop name keeps the value it held when the break ran: in this lesson's searches, the item that matched. A program can read it after the loop to see where the search stopped.
A loop that stops at a match can also end without one. Set a name such as found = False before the loop and make it True right before the break. After the loop, that name says which way the search ended.
4 The rule
A break line ends the nearest loop around it at once, skips the rest of that pass and every later pass, and the program carries on with the first line after the loop.
After a break, a for loop's name keeps its current item.
5 How
- Above the loop, set any name that the program reads after the loop and that only a match changes, such as
found = False. - Write the test that marks a match, and put it in an
ifinside the body. - Inside that
if, do what the match needs, such as settingfound = True, and then writebreak. - Trace the passes until the test is true. At the
break, jump to the first line after the loop. - Check the case with no match. A for loop runs to its end, and a name that only a match changes, such as
found, still holds the value it was given above the loop. Awhile True:loop that reads input and meets no match keeps reading; when the input runs out,input()stops the program with an EOFError.
- Python Software Foundation, Python 3.13 Language Reference, 8.1 The if statement
- Python Software Foundation, Python 3.13 Language Reference, 7.9 The break statement
- Python Software Foundation, Python 3.13 Language Reference, 8.3 The for statement
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: input
6 Worked example
Stop at the first space
for ch in "plan B":
if ch == " ":
print("space found")
break
print(ch)
print("after")
Standard input
Empty
Standard output
p
l
a
n
space found
after
Predict the output first. Then save the code as space.py and run the file. The B after the space is never displayed, because the loop ends at the space.
Read words until done
count = 0
while True:
word = input()
if word == "done":
break
count = count + 1
print(count)
Standard input
fern
moss
done
ivy
Standard output
2
The input holds four lines: fern, moss, done and ivy. The loop counts two words and ends at done, so ivy is never read.
Find the first square over 200
found = False
for n in range(1, 50):
if n * n > 200:
found = True
break
print(found)
print(n)
Standard input
Empty
Standard output
True
15
14 times 14 is 196 and 15 times 15 is 225, so the search stops at 15. After the break, n still holds 15.
- Python Software Foundation, Python 3.13 Tutorial, 4.4 break and continue Statements
- Python Software Foundation, Python 3.13 Language Reference, 8.2 The while statement
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: input
- Python Software Foundation, Python 3.13 Language Reference, 7.9 The break statement
Trace
| Pass | word | count after the pass | Explanation |
|---|---|---|---|
| 1 | fern | 1 | fern is not done, so the if block is skipped and the count goes up to 1. |
| 2 | moss | 2 | moss is not done, so the if block is skipped and the count goes up to 2. |
| 3 | done | 2 | The test matches and break ends the loop before the count line. |
print(count) | done | 2 | The line after the loop displays 2. The fourth line of input is never read. |
7 The trap
A message about a miss written in the body after the match test runs on every pass that misses: in a search for a space, once for each character before the space, or once for every character of a string with no space. Decide after the loop, with a name such as found, whether the search found nothing.
In a while True: loop, the test never ends the loop. If the test before the break is never true for the replies typed, as when each reply is compared with "Done" and the person types done each time, the loop keeps asking. It stops only with an error: an EOFError when the input runs out, or a KeyboardInterrupt when you press Control-C, the interrupt key.
Quick checks
Quick check 1 of 2
Predict what this program displays. The program reads no input.
for ch in "cab":
if ch == "a":
break
print("no match")Show the steps and the answer
It displays no match once.
Why: On the first pass ch holds c, which is not a, so the line no match is displayed. On the second pass ch holds a and break ends the loop, so b is never reached.
Source: Python Software Foundation, Python 3.13 Language Reference, 7.9 The break statement
Quick check 2 of 2
Predict what this program displays when the input lines are 3, 8, 0 and 5.
total = 0
while True:
n = int(input())
if n == 0:
break
total = total + n
print(total)Show the steps and the answer
It displays 11.
Why: 3 and 8 are added to the total. The 0 matches the test, so break ends the loop before 0 would be added, and the 5 is never read.
Source: Python Software Foundation, Python 3.13 Tutorial, 4.4 break and continue Statements
Sources
- Python 3.13 Tutorial, 4.4 break and continue Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 7.9 The break statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.2 The while statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Exceptions: KeyboardInterrupt. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Functions: int. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.