In Python loops (for and while), break and continue are used to control the flow of execution.
break → stops the loop completelycontinue → skips the current iteration and moves to the nextbreak StatementThe break statement is used to exit the loop immediately, even if the loop condition is still True.
for i in range(...):
if condition:
break
for i in range(1, 6):
if i == 3:
break
print(i)
Output
1
2
i == 3, break stops the loopwhilei = 1
while i <= 5:
if i == 4:
break
print(i)
i += 1
for and while loopscontinue StatementThe continue statement is used to skip the current iteration and move to the next iteration of the loop.
for i in range(...):
if condition:
continue
for i in range(1, 6):
if i == 3:
continue
print(i)
Output
1
2
4
5
i == 3, continue skips that iterationwhilei = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
break and continue| Feature | break |
continue |
|---|---|---|
| Function | Stops loop completely | Skips current iteration |
| Loop Execution | Ends immediately | Continues next iteration |
| Use Case | Exit loop early | Skip unwanted values |
for i in range(1, 10):
if i == 5:
break
print(i)
for i in range(1, 10):
if i == 5:
continue
print(i)
break → exits the loop immediatelycontinue → skips the current iteration