break and continue in Python (15/100 Days of Python)
Breaking Out of Loops: The break
Statement
In Python, the break
statement is used to exit or break out of a loop early, before the loop has finished iterating through all of its items. This can be useful when you have a certain condition that you want to check for, and if that condition is met, you want to stop the loop and move on to the next section of your code. It can be used with a for
loop:
for i in range(10):
if i == 5:
break
print(i)
In this code, the for
loop will iterate through the numbers 0 through 9. However, when the value of i
is 5, the if
statement will evaluate to True
, and the break
statement will be executed. This will cause the loop to exit, and the code will continue running after the loop. So the output of this code will be 0,1,2,3,4.
You can also use the break
statement in a while
loop in a similar way:
while True:
n = input('Enter a number or q to quit: ')
if n == 'q':
break
square = int(n) ** 2
print('Square:', square)
In this code, the while
loop will run indefinitely, prompting the user to enter a number. However, if the user enters the letter 'q', the if
statement will evaluate to True
, and the break
statement will be executed. This will cause the loop to exit, and the code will stop running.
It’s important to note that the break
statement only exits the innermost loop that it is in. If you have nested loops, you will need to use the break
statement multiple times to exit all of the loops.
Skipping an iteration: The continue
Statement
In addition to break
statement, Python also has continue
statement which is used to continue to the next iteration of the loop without executing the rest of the code in the current iteration. This can be useful when you have a certain condition that you want to check for, and if that condition is met, you want to skip the current iteration of the loop without stopping the loop entirely:
for i in range(10):
if i % 2 == 0:
continue
print(i)
In this code, the for
loop will iterate through the numbers 0 through 9. However, when the value of i
is even, the if
statement will evaluate to True
, and the continue
statement will be executed. This will cause the loop to skip the current iteration and move on to the next iteration, and so the output of this code will be 1,3,5,7,9.
You can also use the continue
statement in a while
loop in a similar way:
while True:
n = input('Enter a number: ')
if n.isalpha():
continue
square = int(n) ** 2
print('Square:', square)
In this code, the while
loop will run indefinitely, prompting the user to enter a number. However, if the user enters a non-numeric value, the if
statement will evaluate to True
, and the continue
statement will be executed. This will cause the loop to skip the current iteration and prompt the user for input again.
It’s important to note that the continue
statement only affects the innermost loop that it is in. If you have nested loops, you will need to use the continue
statement multiple times to skip the current iteration in all of the loops.
In summary, break
statement is used to exit a loop early, while the continue
statement is used to skip the current iteration of a loop and move on to the next iteration. They both can be used infor
and while
loops, and can be useful when you have a certain condition that you want to check for, and if that condition is met, you want to skip the current iteration (with continue) of the loop or stop it completely (with break).
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course — how to stop loops and how to skip an iteration
- Full series: 100 Days of Python
- Previous topic: While loops
- Next topic: How Python almost had another keyword