Lesson 16 of 60
โฎPrev Nextโฏ

PY-101 ยท Loops

break, continue and loop else

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

16of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    break ends a loop immediately, continue skips the rest of the current round and goes to the next one. A loop can also have an else block, which runs only if the loop finished without hitting break. Together they cover "stop when found" and "skip this one".

    • Stop a loop with break
    • Skip a round with continue
    • Use for ... else for searches

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    break, continue and loop else, running
    1. orders = [1200, -5, 800, 52000, 300]
    2. for amount in orders:
    3. if amount < 0:
    4. print("Skipping bad value", amount)
    5. continue
    6. if amount > 50000:
    7. print("Big order found:", amount)
    8. break
    9. else:
    10. print("No big order")
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou are scanning 1,000 orders for the first one over โ‚น50,000. Once you find it, there is no reason to read the other 999.
    2. The Python ideafor item in items:break ends a loop immediately, continue skips the rest of the current round and goes to the next one.
    3. In real lifeLog scanners skip blank lines with continue and stop at the first critical error with break.

    Start with a question

    You are scanning 1,000 orders for the first one over โ‚น50,000. Once you find it, there is no reason to read the other 999.

    Searching, skipping bad data and stopping early are everyday jobs. These three keywords make them short and clear.

    Explain it like I'm 10

    break is "found it, stop looking". continue is "not this one, next please".

    The technical explanation

    break jumps straight out of the nearest loop. Code after the loop runs next.

    continue jumps back to the top for the next item, skipping the rest of the block for this one.

    for ... else: runs the else block only when the loop ended normally. It is perfect for "not found" messages.

    Syntax

    for item in items:
        if skip_this:
            continue
        if found_it:
            break
    else:
        ...  # no break happened

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Log scanners skip blank lines with continue and stop at the first critical error with break.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does break do?
    2. 2. What does continue do?
    3. 3. When does a for loop's else run?
    4. 4. for i in range(5): if i == 2: continue; print(i). What prints?
    5. 5. for i in range(5): if i == 2: break; print(i). What prints?

    Questions people ask

    Does break exit all loops in Python?

    No, only the innermost loop it is in. To leave nested loops, use a flag, put the loops in a function and return, or restructure the search.

    What is for else in Python used for?

    For searches: the else block runs when the loop finished without break, which means nothing was found.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Does break exit all loops in Python?

    No, only the innermost loop it is in. To leave nested loops, use a flag, put the loops in a function and return, or restructure the search.

    beginner What is for else in Python used for?

    For searches: the else block runs when the loop finished without break, which means nothing was found.

    intermediate What does break do?

    Ends the loop. It leaves the nearest loop immediately.

    intermediate What does continue do?

    Skips to the next round. The rest of this round is skipped.

    Quick revision

    Remember

    • Stop a loop with break
    • Skip a round with continue
    • Use for ... else for searches
    • The else on a loop is easy to misread: it means "no break happened", not "the loop was empty".

    Syntax

    for item in items:
        if skip_this:
            continue
        if found_it:
            break
    else:
        ...  # no break happened

    Official documentation