Break statement

Using Break statement:
A python break statement ends the present loop and instructs the interpreter to starts executing the next statement after the loop. It can be used in both ‘for’ and ‘while’ loops.
Besides leading the program to the statement after the loop, a break statement also prevents the execution of the ‘else’ statement.

pizza=["New York Style Pizza","Pan Pizza","Thin n Crispy Pizza","Stuffed Crust Pizza"]
if choice=="Pan Pizza"
print("Please pay $16.Thank you!")
break
print("Delicious,cheezy"+choice)
else:
print("Cheezy pan pizza is my all-time favorite!")
print("Finally,I'm full!")

OUTPUT:
Delicious,cheezy New York Style Pizza
Please pay $16.Thank you!
Finally,I'm full!


2.Example for break statement:
list =[1,2,3,4] 
count = 1; 
for i in list: 
if i == 4: 
print("item matched") 
count = count + 1; 
break 
print("found at",count,"location"); 
 
Output:
item matched
found at 2 location

3.Example of Break statement in "for loop"
for val in "string":
 if val == "i":
 break
 print(val)
 print("The end")
Translate »