Have you ever found yourself stuck in a loop that just won't quit? In this tutorial, you’ll master the break statement in Python, your primary tool for stopping a for or while loop dead in its tracks. Whether you've found the data you were looking for or hit a specific error condition, the break keyword gives you ultimate control over your program's flow.
How the break Statement in Python Works
When the Python interpreter encounters a break statement, it immediately terminates the current loop. The program "jumps" out of the loop block and moves straight to the very next line of code.
Key Use Cases and Best Practices
The break statement in Python is most effective when paired with an if statement. This allows you to exit a loop only when a specific criteria is met, saving valuable processing time.
- Conditional Exit: Use if to check a condition; if True, trigger the break.
- Search Optimization: Stop searching a list once the target item is found.
- Nested Loops: Note that when using a break inside nested loops, Python only terminates the innermost loop where the statement resides. The outer loops will continue running as usual.
break statement Python examples
1- Using break statement with for loop in Python. In the example a tuple is iterated using a for loop to search for a specific number as soon as that number is found you need to break out of for loop.
numbers = (89, 102, 0, 234, 67, 10, 333, 32)
flag = False
for num in numbers:
if num == 10:
flag = True
break
if flag:
print('Number found in tuple')
else:
print('Number not found in tuple')
Output
Number found in tuple
As you can see here break statement is used inside for loop to break out of loop as soon as the condition is satisfied (Search Optimization use case). Note that searching for an element in a tuple can be done in a more compact way like given below, above example is more of an illustration of break statement.
if searched_num in numbers:
print('Number found in tuple')
else:
print('Number not found in tuple')
2- Using break statement in Python with while loop. In the example there is an infinite while loop that is used to prompt user for an input. Condition here is that entered number should be greater than 10 to break out of while loop otherwise prompt user again to enter valid input.
while True:
num = int(input("Enter a number greater than 10: "))
# condition for breaking out of loop
if num > 10:
break
print("Please enter a number greater than 10...")
print("Entered number is", num)
Output
Enter a number greater than 10: 7 Please enter a number greater than 10... Enter a number greater than 10: 11 Entered number is 11
3- In this example we’ll see how to use break statement with nested loops. There are 2 for loops in the example and break statement is used in the scope of inner for loop so it breaks out of that loop when the condition is true.
for i in range(1, 6):
print(i, "- ", end='')
for j in range(1, 10):
print('*', end='')
# print only 5 times then break
if j >= 5:
break
# move to next line now
print()
Output
1 - ***** 2 - ***** 3 - ***** 4 - ***** 5 - *****
As you can see though the range of inner loop is (1..10) but it breaks out when j’s value is 5 because of the break statement. The end='' used in print statement ensures that the cursor doesn’t move to next line after printing in each iteration.
That's all for this topic Python break Statement With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-