Break Statement in Python

Break Statement in Python

Break statement in Python is used to terminate the loop when the condition associated with the break statement becomes true. When it is used inside a nested loop which is a loop insider another loop, then the break statement terminates the innermost loop. It is different from a continue statement which skips the current iteration only and the loop still continues working after it.

 

 

Break with While Loop

# Python program to exemplify break statement with while loop 
num=1
while (num < 10):
  if (num == 6):
    print("Getting out of the loop.")
    break
  print(num)
  num += 1

In the program provided above, a variable named “num” is created; a while loop is then used to print its value and is initialized to 1. When the value of the variable “num” becomes 6, condition related to break becomes true, and break statement is executed, terminating loop execution. 

Output:

1
2
3
4
5
Getting out of the loop.

 

 

Break statement with For Loop

# Python program to exemplify break statement with for loop 

colors = ['red', 'blue', 'pink', 'yellow', 'black', 'orange'] 
for color in colors:
  if(color == 'yellow'):
    print("Getting out of the loop.")
    break
  print(color)

In the program provided above, a list named “colors” is created. A for loop is then used to iterate over its elements. During iteration, name of elements is displayed. If the element is “yellow”, then the break statement is executed which terminates the entire loop.

Output:

red
blue
pink
Getting out of the loop.

 

 

Break statement with Nested Loop

# Python program to exemplify break statement with nested loop 

# nested loop without break statement	
numbers = [1, 2, 3] 
multipliers = [10, 100, 1000]
print("nested loop without break statement")
for number in numbers:
  for multiplier in multipliers:
    print (number * multiplier)

In the program provided above, a nested loop is used without a break statement. Outer loop is used for the list “numbers” and inner loop is used for the list “multipliers”. For every number in numbers, inner loop is executed and results in multiplication of number with every element of the list multipliers.

Output:

nested loop without break statement
10
100
1000
20
200
2000
30
300
3000

 

# nested loop with break statement  
digits = [1, 2, 3] 
multipliers = [10, 100, 1000]
print("# nested loop with break statement")
for digit in digits:
  for multiplier in multipliers:
    if (multiplier == 100):
      break
    print (digit * multiplier)

In the program provided above, a nested loop is used with a break statement. Outer loop is used for the list “numbers” and inner loop is used for the list “multipliers”. For every number in numbers, inner loop is executed and results in multiplication of number with every element of the list multipliers. If value of multiplier is 10, then it terminates the inner loop for rest of the iterations and execution continues with outer loop.

Output:

nested loop with break statement
10
20
30

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *