Online Tutorials & Training Materials | STechies.com
Register Login

Break Statement in Python with Example

29 Jul 2019 1:04 pm || 0

When you write some code for a program, the main intention is to execute every line of the code. But there may be a situation where you might want to disrupt the normal flow of the program. This can happen when you are executing a for or a while

What is Break in Python?

In Python, the break statement is used for changing the normal functioning of a loop. In loops, a set of statements execute repeatedly until a test condition is satisfied. But in some cases, we need to terminate the current iteration of the loop without reaching the condition.

Break statements are used to remove the control out of the loop in those cases and resume the execution of the next statements outside the loop.

Python Break for Loop

The break statement is used for prematurely exiting a current loop. It can be used for both for and while loops. If the break statement is used inside a nested loop, the innermost loop will be terminated. Then the statements of the outer loop are executed.

The break statement is commonly used along with the if statement so that when the if condition is true, the break statement is executed.

Python Break Flow Diagram

Python Break Statement

Syntax of Python Break Function

Syntax of break statement in Python is:

break

Example of Python Break Statement with for Loop

car = ["Maruti","Hundai","Honda","Polo","Tata"]
print("Car Name from array without break keyword");
for car_name in car:
   print("Car Name : " + car_name)
   print("Break Keyword applied after 'Polo' Car Name");
for car_name in car:
      if car_name == 'Polo':
       break
print("Car Name : " + car_name)

OUTPUT:

Car Name from array without break keyword
Car Name : Maruti
Car Name : Hundai
Car Name : Honda
Car Name : Polo
Car Name : Tata
Break Keyword applied after 'Polo' Car Name
Car Name : Maruti
Car Name : Hundai
Car Name : Honda

Explanation

In the above program, a list ‘car’ is initialized with different car companies name. Then a print() method prints out the string "Car Name from array without break keyword".

In the next line, a for loop is executed to iterate over the elements of the car variable. A variable called car_name is used for iteration. Inside the loop, the print() method prints out the name of the car using the car_name variable.

In the next line, another print() statement prints out the string "Break Keyword applied after 'Polo' Car Name".

Another for loop is executed for looping through the elements of the car variable. An if condition is used for checking the name of the car. When the variable value is Polo, a break statement is executed.

Thus we got the output of all car names until polo is encountered. 

Example of Python Break Statement with while Loop

# Initializing a variable 'i'
i = 1
# Using while loop
while i < 6:
  if (i ==4):
    break
  print("value of i: ",i)  
  i += 1

OUTPUT :

value of i:  1
value of i:  2
value of i:  3

Explanation 

In the above code, we have first initialized a variable ‘i’ with value 1. The while loop should prints the value of  i until it reaches 6. but we used a break statement for the condition if the value of ‘i’ becomes 4 then exit the loop body.

Thus only values until 3 are printed.

Conclusion

As understood from the article, the break statement terminates the loop it is currently in. The statements outside the loop will run normally. So, make sure the most important lines of code are executed and not terminated prematurely by the break condition. Statements such as incrementing the loop using a variable must be coded before the break statement.