Loops allow you to execute a block of code repetitively until it fulfills a specified condition. While loop works in a similar manner and executes the code until the condition is true. “While” loop can be helpful but should be used with consideration. Using while loops without thinking of an exit strategy can result in “infinite” loops. To avoid this issue, Python has a “break” statement that helps in exiting the loop. 

We can break out of the while loop in the following ways:

  • Break the loop based on the user’s input
  • Break the loop with a conditional check

Need of Breaking Out of the While Loop

Before we dive into different ways of breaking out of the while loop, we will look into certain factors that make it important to break out of the loop. Some of these factors are:

  • The most important reason to break the while loops is the infinite loops. It is crucial to stop them from executing forever as they may crash your program or may even freeze your computer
  • Sometimes the loop needs to be terminated if the user gives an invalid input
  • The loop needs to be terminated if it is provided with an external event.

How to Break Out of the while loop in Python?

The “break” keyword in Python is used to stop the loop from operating. Whenever the break statement is found in the while loop, it stops immediately. The program continues from the next line of code after getting out of the loop.

Let’s look at some examples of how we can break out of the while loop in Python.

Example 1: Breaking Loops Based on User’s Input

In this example, the loop runs until the user inputs “exit”. As soon as the user inputs “exit”, the loop gets broken:

while True:
    user_input = input("Please enter the required number (or 'exit' to quit): ")
   
    if user_input == 'exit':
        break
       
    numb = int(user_input)
    print("Double of the number:", numb + numb)

In this code,

  • The input is taken from the user and stored in the variable user_input”. The code will run while the condition is true.
  • The user input is stored in the variable ‘numb’
  • The result that will get printed is double the number.
  • If the user input is “exit”, the loop will break.

Output

The output shows that the loop breaks as soon as the user enters “exit”:

Example 2: Breaking Loop With Conditional Check

In this example, the loop keeps running until a certain break condition is met.

count = 0

while count < 15:
    print(count)
   
    if count == 13:
        break
       
    count = count+1

In the above code,

  • ‘Count’ is initialized with the value of 0
  • The loop will keep on running until the value of count is lesser than ‘15’
  • It will print out the value of count that will keep on incrementing until the value of count is ‘13’. At this point, the loop will break off. 

Output:

The output of the code is:

Things to Remember

Some things to remember while handling while loops are:

  • Make sure that there is a defined exiting strategy to avoid the infinite loops
  • Using the ‘break’ statements carefully in nested loops because one may break off from the wrong loop resulting in unwanted behavior

Conclusion

In this article, we have learned about why is it necessary to break off from the loop and what are some ways we can break out of while loop in Python. So, we can break off the loop using the ‘break’ statement. While loops are very useful but they need to be controlled to avoid the scenario of infinite loops. It is always advisable to cautiously handle the while loops to improve the efficiency of code.