Python break and continue
break exits the loop immediately. continue skips the rest of the current iteration and moves to the next one.
Using break
Stop the loop when a specific condition is met.
for num in range(1, 10):
if num == 5:
break
print(num)
Try it Yourself →
Using continue
Skip the current iteration and move to the next one.
for num in range(1, 8):
if num % 2 == 0:
continue
print(num)
Try it Yourself →
break in Nested Loops
break only exits the innermost loop it is in.
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
break
print(f"({i},{j})")
Try it Yourself →