Skip to content

break and continue

break statement

The break statement is used to exit a loop prematurely when a certain condition is met.

Examples:

# Using break to exit the loop when a condition is met
for number in range(1, 10):
if number == 5:
break # Exit the loop when number is 5
print(number) # This will print numbers 1 to 4

continue statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration.

Examples:

Here are some examples using continue statements

# Using continue to skip even numbers
for number in range(1, 10):
if number % 2 == 0:
continue # Skip even numbers
print(number) # This will print only odd numbers