On this page

Loop Control (break/continue)

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Why Loop Control Matters​

Loops normally run until their sequence ends (for) or their condition becomes False (while). Sometimes you need finer control: stopping a loop early or skipping part of its body. Python provides two statements for this purpose: break and continue. They allow you to manage loop behavior precisely and write clearer, more intentional logic.

The break Statement​

break immediately stops the loop. Python exits the loop entirely and continues with the next statement after the loop.

Breaking Out of a While Loop​


while True:

    text = input("Type 'stop' to exit: ")

    if text == "stop":

        break



print("Loop ended.")

The loop would normally run forever, but break provides a controlled exit.

Breaking Out of a For Loop​


for n in range(10):

    if n == 5:

        break

    print(n)

The loop stops as soon as n reaches 5. Values 5–9 are never processed.

When to Use break​

The continue Statement​

continue skips the rest of the current iteration and moves directly to the next one. The loop itself does not end.

Skipping Values in a For Loop​


for n in range(6):

    if n == 2:

        continue

    print(n)

The number 2 is skipped, but the loop continues with 3, 4, and 5.

Skipping Work in a While Loop​


count = 0



while count < 5:

    count += 1

    if count == 3:

        continue

    print(count)

The loop still runs five times, but the body is skipped when count is 3.

When to Use continue​

Combining Loop Control with Conditions​

Loop control statements are most effective when combined with Boolean logic and conditional statements.


numbers = [3, -1, 7, 0, -5, 9]



for n in numbers:

    if n < 0:

        continue      # Skip negative numbers

    if n == 0:

        break         # Stop entirely when zero appears

    print("Positive:", n)

This example demonstrates selective processing and early termination in a single loop.

Loop Control and Readability​

break and continue are powerful tools, but they must be used intentionally. Overusing them can make loops harder to follow. Use them when they make the logic clearer, not more complicated.

Why Loop Control Is Essential​

Loop control allows you to:

Mastering break and continue prepares you for more advanced control‑flow patterns and real‑world programming tasks.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

While Loops

Next

Error Handling