Quarry School

Stop a search at a match

Subject: Computing
Level 1 · FoundationsAbout 6 min read1 new term
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    Someone looking for keys opens one drawer after another.

    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.

    Step

    1. Above the loop, set any name that the program reads after the loop and that only a match changes, such as found = False.

    2. Write the test that marks a match, and put it in an if inside the body.

    3. Inside that if, do what the match needs, such as setting found = True, and then write break.

    4. Trace the passes until the test is true. At the break, jump to the first line after the loop.

    5. 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. A while True: loop that reads input and meets no match keeps reading; when the input runs out, input() stops the program with an EOFError.

    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.

    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.

    2 Picture it

    PasschWhat happens1 to 4p l a ndisplayed5spacebreakafterspaceB not reached

    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.

    The passes of the search for a space in plan B

    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

    1. Above the loop, set any name that the program reads after the loop and that only a match changes, such as found = False.
    2. Write the test that marks a match, and put it in an if inside the body.
    3. Inside that if, do what the match needs, such as setting found = True, and then write break.
    4. Trace the passes until the test is true. At the break, jump to the first line after the loop.
    5. 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. A while True: loop that reads input and meets no match keeps reading; when the input runs out, input() stops the program with an EOFError.

    6 Worked example

    Stop at the first space

    Python code
    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

    Python code
    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

    Python code
    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.

    Trace

    Trace the loop that reads words until done
    Passwordcount after the passExplanation
    1fern1fern is not done, so the if block is skipped and the count goes up to 1.
    2moss2moss is not done, so the if block is skipped and the count goes up to 2.
    3done2The test matches and break ends the loop before the count line.
    print(count)done2The 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.

    Python code
    for ch in "cab":
        if ch == "a":
            break
        print("no match")
    Show the steps and the answer
    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.

    Python code
    total = 0
    while True:
        n = int(input())
        if n == 0:
            break
        total = total + n
    print(total)
    Show the steps and the answer
    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

    1. Python 3.13 Tutorial, 4.4 break and continue Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Language Reference, 7.9 The break statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    5. Python 3.13 Language Reference, 8.2 The while statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    6. Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    7. Python 3.13 Library Reference, Built-in Exceptions: KeyboardInterrupt. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    8. Python 3.13 Library Reference, Built-in Functions: int. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.