break Statement in Python

This tutorial is created to cover one of the second most used conditional statement named break in Python. Here I've provide you all the details about break keyword or statement along with its simple examples. This article deals with:

  • What is break statement ?
  • Syntax of break statement
  • Examples of break statement
  • break statement in while loop
  • break statement in nested while loop
  • break statement in for loop

What is break Statement ?

The break statement in Python is used to exit a loop. In other words, we use break keyword to terminate the remaining execution of the whole/complete loop in its indentation. Don't worry about the definition, you'll get to know everything about it, after understanding its examples given below.

Syntax of break Statement in Python

The syntax to use break keyword is only the keyword itself. Therefore, its syntax is:

break

That's it. We do not need anything else other than the keyword itself.

Examples of break Statement in Python

Now let's have a look at some of the examples on break keyword or statement. Let's start with break statement in while loop example.

break Statement in while Loop

This program contains break inside the while loop.

count = 0
while True:
    count = count+1
    if count>10:
        break
    print(count)

This program produces the output like shown in the snapshot given below:

python break statement

As you can see from the above output, when the variable count becomes greater than 10. That is, when the value of count becomes 11, then the condition count>10 evaluates to be True, so the program flow goes inside the if's body and the statement break gets executed, that leaves the loop for further execution or simply terminates the remaining execution of while loop.

Let's take another example of break keyword/statement, that helps a lot to you, in understanding the benefit of using break in Python:

num = 2
while True:
    mul = 1
    print("\n-----Table of", num, "-------")
    while mul<=10:
        print(num, "x", mul, "=", num*mul)
        mul += 1
    print("\nWant to continue printing Table ? (y/n) ", end="")
    confirm = input()
    if confirm == 'n':
        break
    num += 1

This is the initial output produced by above program:

python break keyword

After printing the table of 2, the program asks from user, whether he/she wants to print next table or wants to skip or stop printing of table. To confirm, enter y to print next table, otherwise enter n to stop. For example, the output given below produces after providing y as input:

break keyword example python

Since, the condition of while loop is given is True, that always evaluates to be True, therefore the execution of the loop, never ends, until and unless you use the break keyword. And to do this, we have to enter n as input, so that the condition confirm=='n' evaluates to be True and the program flow goes inside the if's body and executes the break keyword, that helps in exiting/leaving the loop, like shown in the sample run given below:

python break keyword example program

break Statement in Nested while Loop

This program uses break keyword in a while loop present inside another while loop:

count = 0
while count<10:
    count = count+1
    while count<5:
        if count==3:
            break
        count = count+1
    print(count)

This program produces following output:

break statement in python

The execution of above program goes like:

  • First 0 gets initialized to count
  • Now the condition count<10 or 0<10 evaluates to be True
  • Therefore program flow goes inside the body of the while loop
  • Inside the body of loop, the first statement, that is count = count+1 gets executed
  • Therefore count+1 or 0+1 or 1 gets initialized to count
  • Now the condition count<5 or 1<5 evaluates to be True
  • Therefore program flow goes inside the body of the loop (the inner while loop)
  • And the condition count==3 or 1==3 evaluates to be False
  • Therefore program flow does not goes inside the body of if block
  • Rather the statement count = count+1 (inside inner loop) gets executed
  • That is count+1 or 1+1 or 2 gets initialized to count
  • Since all the statements of inner loop gets executed
  • Then program flow again evaluates the condition
  • Therefore the condition count<5 gets evaluated again
  • This time, again count<5 or 2<5 evaluates to be True
  • Therefore program flow again goes inside the loop
  • And the condition count==3 or 2==3 again evaluates to be False
  • Now count+1 or 2+1 or 3 gets initialized to count
  • Program flow again evaluates the condition of inner loop. This process continues, until the condition evaluates to be False
  • That is, the condition count<5 or 3<5 again evaluates to be True
  • Therefore program flow again goes inside this loop
  • But this time, the condition of if, that is count==3 or 3==3 evaluates to be True
  • Therefore program flow goes inside this if's body
  • And using the break keyword, we've leaved the loop (inner while loop)
  • That is, using break, the remaining execution of its nearest parent loop gets terminated or stopped
  • After exiting the loop, using the print() statement, the value of count gets printed. That is 3
  • As all the statements of outer while loop gets executed
  • Therefore its condition again gets evaluated. This process continues until the condition evaluates to be False
  • That is, the condition count<10 or 3<10 evaluates to be True again
  • Therefore program flow again goes inside the loop and count+1 or 3+1 or 4 gets initialized to count
  • And the condition of inner loop, count<5 gets evaluated
  • That is, the condition count<5 or 4<5 evaluates to be True
  • Therefore program flow inside this inner loop
  • Inside this loop, the condition (of if) count==3 or 4==3 evaluates to be False
  • Therefore program flow does not goes inside its body. Rather it increments the value of count
  • That is, the value of count now equals to 5
  • And this time the condition of inner loop, that is count<5 or 5<5 evaluates to be False
  • Therefore, the inner while loop's execution stops from now. Because every time from now, the condition evaluates to be False
  • And now the value from 5 to 10 gets printed on output one by one
  • That is, the value 1, 2, and 4 does not gets printed

break Statement in for Loop

The program given below uses break statement in for loop:

for i in range(10000000):
    if i==5:
        break
    print(i)

Here is its sample output:

python break statement example

As you can see, using break statement, I've stopped the remaining execution of the loop when the value of i becomes equal to 5.

Let's take another example that shows you the most important use of break statement:

nums = list()
print("Enter 5 numbers: ")
for i in range(5):
    nums.append(int(input()))

print("\nEnter a number to search: ")
n = int(input())

for i in range(5):
    if n==nums[i]:
        print("\nItem found at Position:", i+1)
        break

Here is its sample run with user inputs, 11, 22, 33, 44, 55 as five numbers and 22 as number to search:

break keyword in python

Note - The break keyword helps a lot to save unwanted execution of codes. As you can see from above program, when our work got finished, that is, when the element gets found. Then we don't need to execute or search with further remaining elements. Therefore using break keyword, we simply exit from the loop. That's it.

Python Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube