Using loops is an essential part of coding. They help you iterate a task a given condition is fulfilled. But situations may arise where you may skip one iteration, ignore one iteration, or exit from the loop completely. These processes can be done using Python continue statement, python pass statement, and the break statement respectively. Generally, they are called loop control statements. In this tutorial, we will focus on the Python break statement. You will understand when to use the break statement in for and while loops. There will be a couple of examples in this tutorial so you can truly understand how the break statement works and situations where it is imperative to use it.
Let hop into it. We will begin with the fundamental question.
Table of Contents
What is Python Break Statement?
A break statement is used to terminate a loop when some condition defined within the loop is met. The break statement can be used for both for and while loops. They are generally placed inside the looping block.
If a break statement is used in a nested loop, it breaks out of the innermost loop and continue execute the line of code after that line, ie. The external loop.
Let’s see a couple of use cases where the break statement can be used.
Use Case 1: Using Break in Python with a For Loop
#create a list of numbers
list = range(15)
#use a for loop to traverse the loop
for i in list:
#print each iteration output
print(i, end=' ')
#breaks out of the loop if i is equal to 13
if i == 13:
#once the condition is met, print condition met
print('Condition met')
break
print(f'The program breaks out of the loop after the {i}th iteration')
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 Condition met
In the above example, the loop begins from 0 and continues until the condition (looping variable = 13) is met. The program breaks out of the loop after the 13th iteration
Let’s see another example
#create an string
list = "PythonTutorials"
#use a for loop to traverse the loop
for i in list:
#print each iteration output
print(f'The current letter is {i}')
#breaks out of the loop if i is equal to 13
if i == 'i':
#once the condition is met, print condition met
print('Condition met')
break
print(f'The program breaks out of the loop')
Output:
The current letter is P
The current letter is y
The current letter is t
The current letter is h
The current letter is o
The current letter is n
The current letter is T
The current letter is u
The current letter is t
The current letter is o
The current letter is r
The current letter is i
Condition met
The program breaks out of the loop
In the above example, the python line break was used to exit the for loop once it receives the letter ‘i’. In practical terms, you can use the break statement terminate a guessing game once the current answer is inputted
Use Case 2: Using the Python Break Statement with While Loops
The break statement also terminates a while loop when the break condition is met. Let’s treat some examples.
#define a finite number
i = 15
#create a while loop that checks from 0
while i > 0:
#print the number the loop transverses
print(f'The current number is {i}')
#reduce the number by 1
i -= 1
if i == 3:
print('The loop terminates')
#break out of the loop
break
print('This is outside the while loop')
Output:
The current number is 15
The current number is 14
The current number is 13
The current number is 12
The current number is 11
The current number is 10
The current number is 9
The current number is 8
The current number is 7
The current number is 6
The current number is 5
The current number is 4
The loop terminates
This is outside the while loop
Let’s see another example
#create an string
list = "PythonTutorials"
#create an count index
count_index = 0
#use a while loop to traverse the loop
while count_index < len(list):
#print each iteration output
print(f'The current letter is {list[count_index]}')
count_index += 1
if list[count_index] == 'i':
print('Condition met')
break
print(f'The program breaks out of the loop after the {count_index}th iteration')
Output:
The current letter is P
The current letter is y
The current letter is t
The current letter is h
The current letter is o
The current letter is n
The current letter is T
The current letter is u
The current letter is t
The current letter is o
The current letter is r
Condition met
The program breaks out of the loop after the 11th iteration
Use Case 3: Using the Break Statement in Nested Loops
A break statement can be used in nested loops as well, i.e a loop inside another loop. When you use a break statement in a loop, it breaks out of the inner loop but gets on with the inner loop. Let’s take an example. In the example, we will be building a multiplication table using nested for loops. We will then use the python break statement to limit the multiplication taken to the 5th multiplier. Take a look.
#create a for loop that iterates from 1 to 5
for i in range(1, 6):
#create another for loop that iterates from 1 to 11
for j in range(1, 12):
#multiply the first loop element by the second loop element
print(f'{i} * {j} = {i * j}', sep='\t')
if j == 5:
print('End of inner loop')
#Stop the second loop at number 5
break
print('End of outer loop')
Output:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
End of inner loop
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
End of inner loop
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
End of inner loop
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
End of inner loop
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
End of inner loop
End of outer loop
Without the break statement, the multiplication should have run from 1 to 12. It however stops at 5 because we stated that the inner loop should be terminated when the element is equal to 5. At that point it printed ‘End of inner loop’. Note that the continued the outer and terminated only after it exhausted the list items. The last item in range(1, 6) is 5. This is why the code stops at 5 multiplied by 5.
In summary, you have seen how to use the python break statement to control for loops and while loops. Note that since you’ll mostly use the break statement after a condition is met, break in python programs will most likely come after an if statement.