0
0
Pythonprogramming~5 mins

Break vs continue execution difference in Python

Choose your learning style9 modes available
Introduction

Break and continue help control loops by deciding when to stop or skip steps.

Stop a loop early when a condition is met, like finding a match in a list.
Skip certain steps in a loop, like ignoring unwanted items.
Exit a loop when an error or special case happens.
Avoid unnecessary work by stopping or skipping parts of a loop.
Syntax
Python
break
continue

break stops the whole loop immediately.

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

Examples
This loop stops completely when i is 3.
Python
for i in range(5):
    if i == 3:
        break
    print(i)
This loop skips printing when i is 3 but continues with other numbers.
Python
for i in range(5):
    if i == 3:
        continue
    print(i)
Sample Program

This program shows how break stops the loop at 4, and continue skips printing number 2.

Python
for i in range(6):
    if i == 4:
        print('Break at', i)
        break
    if i == 2:
        print('Continue at', i)
        continue
    print('Number', i)
OutputSuccess
Important Notes

Use break to stop loops early when you have what you need.

Use continue to skip only certain steps without stopping the whole loop.

Both help make loops more efficient and clear.

Summary

break stops the entire loop immediately.

continue skips the current step and moves to the next one.

Both control loop flow to handle special cases easily.