Python‎ > ‎Control Flow‎ > ‎

break






































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Definition
The statement terminate the loop and start the execute of code in next statement even if the condition has not become false. If break statement is subject to if statement, then break will only execute when the condition has become false.

Syntax
Break_stmt -> break loop

Parameters
None

Example
for loop subject to break where x is equal 8.
 for x in range(1,10):
    print(x)
    if x==8:
        break
    else:
        print(x)

>>> 
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
>>> 


While....: if....: ..... except:  break
The loop returns with: 
 while True:
    try:
        print("Please enter password: ")
        x=input()
        password=int(x)
        print("Please enter correct password: ", password, "is not correct")
    except:
        print("password: " ,password, "is not correct")
        break    

>>> 
Please enter password: 
152
Please enter correct password:  152 is not correct
Please enter password: 

Comments