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)
Example: Using Break in a While Loop
x = 0
while x < 10:
if x == 5:
break
print(x)
x += 1
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)
Example: Using Continue in a While Loop
x = 0
while x < 5:
x += 1
if x == 3:
continue
print(x)
Key Differences Between Break & Continue
- break - Exits the loop completely.
- continue - Skips the rest of the current iteration and proceeds to the next.