Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python break, continue, and Loop else

Control flow · Tutorial 13

Python break, continue, and Loop else

Control loop execution with break and continue, and learn how the rarely used loop else clause works.

Open the compilerPractice exercises
beginner6 min readUpdated November 19, 20263 learning goals
On this page
  1. break
  2. continue
  3. break out of nested loops
  4. Loop else
  5. Practice

What you will learn

  • Exit a loop early with break
  • Skip an iteration with continue
  • Understand when a loop's else block runs
Before this lesson: Python for Loops, Python while Loops

Most loops run until their condition or iterable is exhausted. Two statements let you change that behaviour from inside the loop.

break#

break exits the nearest enclosing loop immediately. No further iterations run.

Python
numbers = [4, 8, 15, 16, 23, 42]

for number in numbers:
    if number > 10:
        print("First value above 10:", number)
        break
Expected output

First value above 10: 15

Values after 15 are never visited. This is useful for searching:

Python
names = ["Ada", "Grace", "Linus"]
target = "Grace"

for name in names:
    if name == target:
        print("Found", target)
        break
else:
    print("Not found")

continue#

continue skips the rest of the current iteration and moves to the next one.

Python
for number in range(1, 8):
    if number % 2 == 0:
        continue
    print(number)
Expected output

1 3 5 7

Only odd numbers reach the print(). The loop itself continues normally.

break out of nested loops#

break affects only the innermost loop:

Python
for row in range(3):
    for column in range(3):
        if column == 1:
            break
        print(row, column)

For each row, only column 0 is printed because the inner loop stops at column 1. If you need to leave both loops, set a flag and check it in the outer loop, or move the search into a function and return.

Python
def find_pair(rows, columns, target):
    for row in range(rows):
        for column in range(columns):
            if row * column == target:
                return row, column
    return None

print(find_pair(5, 5, 6))

Loop else#

A for or while loop may have an else block. It runs only when the loop finishes without hitting break.

Python
numbers = [3, 7, 11]

for number in numbers:
    if number % 2 == 0:
        print("Found an even number")
        break
else:
    print("No even numbers")
Expected output

No even numbers

If the list contained an even number, break would skip the else. The else is not a fallback for an empty loop; an empty loop still runs it.

Use loop else for search

The for ... else pattern expresses "search, and report if nothing matched" without an extra flag variable. If the else confuses readers, a flag is perfectly acceptable.

Practice#

Write a program that reads numbers until it encounters a negative value, then prints the sum of the non-negative numbers. Use break for the stopping condition and continue to skip zeros if you like. Predict what happens if the input contains only non-negative numbers and the lines run out.

Previous tutorialPython range() and enumerate()Next tutorialPython Lists: Creation, Indexing, and Iteration

Related tutorials

beginner

Python Functions: Define, Call, and Return Values

Write reusable code with def, pass arguments, return results, and use docstrings to document behaviour.

9 min read
intermediate

Python Error Handling with try, except, else, and finally

Read tracebacks, catch specific exceptions, raise your own errors, and clean up resources reliably.

9 min read

On this page

  1. break
  2. continue
  3. break out of nested loops
  4. Loop else
  5. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.