Python Break and Continue

Break Statement

The break statement is used to exit a loop prematurely when a condition is met.

Example: Using Break in a For Loop

for num in range(10):
    if num == 5:
        break
    print(num)

Try It Now

Example: Using Break in a While Loop

x = 0
while x < 10:
    if x == 5:
        break
    print(x)
    x += 1

Try It Now

Continue Statement

The continue statement skips the current iteration and moves to the next one.

Example: Using Continue in a For Loop

for num in range(5):
    if num == 2:
        continue
    print(num)

Try It Now

Example: Using Continue in a While Loop

x = 0
while x < 5:
    x += 1
    if x == 3:
        continue
    print(x)

Try It Now

Key Differences Between Break & Continue

  • break - Exits the loop completely.
  • continue - Skips the rest of the current iteration and proceeds to the next.