Python break statement is used to stop a loop immediately when a condition is met. In this chapter, you will learn about the Python break statement, how it works, and how to use it in loops.
The break is a keyword used to exit a loop early, before it finishes all its iterations or meets its condition. When the break statement is executed, the loop stops immediately, and control moves to the next line of code after the loop. It is commonly used when we want to stop the loop based on a specific condition.
The syntax of the break statement in Python is as follows:
Syntax Explanation
When the break statement is executed, it immediately stops the loop and moves control to the next line of code after the loop. In nested loops, break only stops the innermost loop where it is used.
Here is a simple example to understand how the break statement works in Python.
In the following example, we are stopping the loop when the value becomes 6.
Output:
1 2 3 4 5
Explanation:
In this example, the loop runs from 1 to 10. When the value of num becomes 6, the break statement stops the loop. As a result, only numbers from 1 to 5 are printed.
The image below shows the working of the break statement:

Step 1: Start the loop
Step 2: Check the loop condition
Step 3: If the loop condition is False, exit the loop.
Step 4: If the loop condition is True, proceed to the loop body.
Step 5: Inside the loop, evaluate a condition to decide whether to break.
Step 6: If the break condition is True, execute the break statement - immediately exit the loop.
Step 7: If the break condition is False, continue executing the loop body.
Step 8: After completing the current iteration - Go to the next iteration.
Step 9: Repeat steps 2-8 until the loop ends naturally or via break
Let us now see some more examples showing the use of the break statement in Python.
The 'for' loop is used to iterates over a given sequence (e.g., list, tuple, string or range), and executes a block of code for each item in the sequence. We can use the break statement within the 'for' loop in order to terminate the loop before it iterates over all elements, on the basis of a specified condition.
We will take a look an example show how to find an element from a list using the for-loop and the break statement.
Output:
Fruit found! Located at index = 4
Explanation:
In the above example, we are given a list consisting of some fruits. We used the 'for' loop to iterate through the list. We used the enumerate() function to add a counter to the list. Inside the loop, we used the 'if' statement to check if the current element is 'kiwi'. We then break the loop using the 'break' statement. At last, we printed the index value of the located element.
The 'while' loop repeatedly executes a code block as long as a particular condition is True. We can use the break statement within a while loop in order to exit the loop on the basis of dynamic conditions that may not be known in advance.
Let us see an example to understand how to use the break statement with the while loop.
Output:
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 Condition met! Exiting loop.
Explanation:
In this example, we initialize a variable, 'count' with 1 and used the 'while' loop that will run infinitely until the condition is True. Inside the 'while' loop, we printed the current value of the 'count' variable. We used the 'if' conditional statement to check if count's value is 5 and used the 'break' statement to break the loop. At last, we incremented the value of the variable by 1.
Nested loops are the loops within loops. It allow us to perform more complex iterations, like looping over multi-dimensional data structures. While using the break statement within nested loops, it is important to understand its scope.
Here is an example of using the break statement in nested loops to search for a number in a 2D list:
Output:
Number 30 found! Exiting loops.
Explanation:
In this example, we are given a 2D list and a value to be searched in the list. We initialized a flag as False and used the nested 'for' loop to traverse the elements of the 2D list. We then used the 'if' statement to check for the target value in the list and used the 'break' statement to break the inner loop, once found. Then we again used the 'break' statement to exit the outer loop.
We request you to subscribe our newsletter for upcoming updates.