Python:Python Break, Continue, and Pass Loop Control Statements
Python•Loop Control

Python Break, Continue, and Pass Loop Control Statements

break immediately terminates the enclosing loop. continue skips the rest of the current iteration and jumps to the next cycle. pass does nothing.

Syntax

break # Exit loop immediately continue # Skip to next iteration pass # Null placeholder

Parameters & Values

Key components and acceptable values for this structure:

ParameterTypeDescription
breakKeywordExits loop completely.
continueKeywordSkips remainder of current iteration.
passKeywordSyntactic placeholder for empty code blocks.

Ready-to-Run Code Example

Executable in Compilra

Run this complete, working example directly without installing Python locally:

# Demonstrating break and continue
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print("Printing odd numbers up to 7:")
for n in numbers:
    # Skip even numbers
    if n % 2 == 0:
        continue
    # Stop when number exceeds 7
    if n > 7:
        break
    print(f"-> {n}")
Expected Output:
Printing odd numbers up to 7:
-> 1
-> 3
-> 5
-> 7

How It Works: Step-by-Step

  • Lines 7-8: continue skips even numbers 2, 4, 6, 8.
  • Lines 10-11: break halts loop execution as soon as n reaches 9.

Key Concepts

  • continue skips straight to the next sequence item without running subsequent lines.
  • break terminates the innermost loop cleanly.
  • Loops can feature an optional else: block which executes only if break was not triggered.

Common Mistake: Placing code after break within the same branch

Statements placed immediately after break in a block are unreachable.

❌ Incorrect
if item == target:
    break
    print("Found!") # Unreachable code
✅ Correct
if item == target:
    print("Found!")
    break

Related Python Tutorials & Compiler

Run and test Python on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

Python Online Compiler