Introduction

Python is a versatile programming language used extensively across a number of industries and applications. One of the most powerful tools that Python offers is the ability to control the flow of execution within a program. One key feature of controlling flow is the use of the 'break' statement. The break statement is used in Python to exit a loop prematurely. Often times, when working with loops, we want to stop iterating through the loop once a certain condition is met or when a certain value is found. This is where the break statement comes in handy. In this tutorial, we will delve deep into the concept of the break statement, exploring its syntax, functionality, and applications in real-world examples.

Table of Contents :

  • Break statement in  Python
    • Using break statement with for loop 
    • Using break statement with while loop 
    • Using break statement with nested loops 

Break statement in  Python

  • Sometimes in our code, need arises to terminate a loop (for loop or while loop) prematurely.
  • The loop needs to be terminated even if the condition of the loop evaluates to true.
  • The break statement comes into play in such scenarios.
  • When the break statement is executed the control immediately comes out of the loop and rest of the iterations are not executed.
  • Most often the break statement is used with an if condition because we want to terminate the loop only if some condition is met.

Using break statement with for loop 

  • The syntax of the break statement with for loop is as follows :

for cnt in range(n):
	For-code-block
	
	if Break-condition:
 		break

outer-code-block

  • Code sample : break statement with for loop 

for index in range(5):
    sqr = index ** 2
    print(f"Value of index = {index}")
    print(f"Square of index = {sqr}")
    print()

    if index % 3 == 1:
        print()
        print("break condition True.")
        break

print()
print("Coming out of for loop prematurely")
print(f"Value of index = {index}")


# Output
# Value of index = 0
# Square of index = 0

# Value of index = 1
# Square of index = 1


# break condition True.

# Coming out of for loop prematurely
# Value of index = 1

Using break statement with while loop 

  • The syntax of the break statement with while loop is as follows :

while condition:
	While- code-block
	
	if Break-condition:
 		break

outer-code-block

  • Code sample : break statement with while loop 

index = 0
while index < 5:
    sqr = index ** 2
    print(f"Value of index = {index}")
    print(f"Square of index = {sqr}")
    print()

    if index % 3 == 1:
        print()
        print("break condition True.")
        break

    index += 1

print()
print("Coming out of while loop prematurely")
print(f"Value of index = {index}")

# Output
# Value of index = 0
# Square of index = 0

# Value of index = 1
# Square of index = 1


# break condition True.

# Coming out of while loop prematurely
# Value of index = 1

Using break statement with nested loops 

  • In case of nested loops, the break statement terminates the iterations of the inner loop.
  • The syntax of the break statement with nested loop is as follows :

for cnt in range(n):
	for index in range(x):
 		inner-code-block
 		if Break-condition:
  			break
 	
 	outer-code-block
 
 
  • Code sample : break statement in case of nested loops

for index in range(2):
    print("-------------------------------------")
    print("Outer Loop")
    print(f"Value of index = {index}")

    print("Inner Loop")
    for cnt in range(4):
        print(f"Value of cnt = {cnt}")
        if cnt % 3 == 1:
            print("break condition True.")
            break

print()
print("Outer for loop completes normally.")
print(f"Value of index = {index}")

# Output

# -------------------------------------
# Outer Loop
# Value of index = 0
# Inner Loop
# Value of cnt = 0
# Value of cnt = 1
# break condition True.
# -------------------------------------
# Outer Loop
# Value of index = 1
# Inner Loop
# Value of cnt = 0
# Value of cnt = 1
# break condition True.

# Outer for loop completes normally.
# Value of index = 1


Prev. Tutorial : Do...while imitation

Next Tutorial : Continue statement