he break and continue statements are used to control the flow of loops in Python.
They help you stop a loop early or skip certain iterations.
These statements can be used inside both for loops and while loops.
BREAK STATEMENT
The break statement immediately stops the loop.
When break is executed, the loop ends and control moves to the next line after the loop.
Syntax
break
Example with For Loop
for i in range(1, 6):
if i == 4:
break
print(i)
Output:
1
2
3
The loop stops when i becomes 4.
Example with While Loop
num = 1while num <= 5:
if num == 3:
break
print(num)
num += 1
The loop ends when num reaches 3.
CONTINUE STATEMENT
The continue statement skips the current iteration and moves to the next one.
It does not stop the loop completely.
Syntax
continue
Example with For Loop
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
The number 3 is skipped.
Example with While Loop
num = 0while num < 5:
num += 1
if num == 2:
continue
print(num)
The number 2 is skipped.
DIFFERENCE BETWEEN BREAK AND CONTINUE
Break â Stops the loop completely
Continue â Skips one iteration and continues the loop
WHEN TO USE BREAK
⢠When a required condition is met
⢠When searching for something in a loop
⢠When you want to exit early
WHEN TO USE CONTINUE
⢠When you want to skip specific values
⢠When filtering data inside a loop
⢠When ignoring unwanted conditions
Understanding break and continue gives you better control over loop execution and helps in writing efficient Python programs.