codescracker
python

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 ?

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:

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 »



© Copyright 2021. All Rights Reserved.

CodesCracker