Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Break Statement in Python with Example

|| 0

Break Statement in Python with Example
Stechies

What is Break in Python?

In Python, the break statement is used for changing the normal functioning of a loop. In loops, as 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 if the condition is true, only then break 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 

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:
        print("Car Name : " + car_name)
        if car_name == 'Polo':
            break

    print("For Loop exits without completing iterations and continue this statement because of 'BREAK KEYWORD'")

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
Car Name : Polo
For Loop exits without completing iterations and continue this statement because of 'BREAK KEYWORD' 

 


Advertisement
Advertisement
Advertisement