Break, Continue and Pass
Break Statement
Python break Statement uses for the conditionally exit from the loop flow according to the user, Break rapidly terminates the area of the loop.
If the loop is used in a nested loop at that time, the nested loop will be terminated and come into the outer loop, here is the diagram of the break statement.
Diagram
Syntax
break
Program
for x in "salesforcedrillers": if(x=="c"): break print(x,end=" ")
When the condition is true then loop has terminated.
Output:
s a l e s f o r
Break using in the nested loop
Program:
for x in range(1): for y in range(1,10): if(y==5): print("Break Point") break print("Inner Loop") print("Outer Loop")
Here you can see that when inner loop terminated by the break it come out at outer loop
Output:
Inner Loop Inner Loop Inner Loop Inner Loop Break Point Outer Loop
Break using with while loop
Program:
i=1 while(i<=10): if(i==9): print("Break Point") break print(i,end=" ") i+=1
Output:
1 2 3 4 5 6 7 8 Break Point
Continue Statement
Continue statement is used for the skipping the value if the conditional will be satisfy, basically this statement has used when we want to skip the any condition. Here is the diagram which will clearly show the function of the Continue
Diagram
Program:
for x in "salesforcedrillers": if(x=="l"): continue print(x,end=" ")
Output:
= RESTART: C:/Users/laptop/AppData/Local/Programs/Python/Python38-32/break.py s a e s f o r c e d r i e r s >>>
Program:
for x in range(1,100): if(x%2!=0): continue print(x,end=" ,")
Output:
= RESTART: C:/Users/laptop/AppData/Local/Programs/Python/Python38-32/break.py 2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 ,32 ,34 ,36 ,38 ,40 ,42 ,44 ,46 ,48 ,50 ,52 ,54 ,56 ,58 ,60 ,62 ,64 ,66 ,68 ,70 ,72 ,74 ,76 ,78 ,80 ,82 ,84 ,86 ,88 ,90 ,92 ,94 ,96 ,98 >>>
Pass Statement
Pass statement is like null if you do not want to do anything then declare it as Pass. it is like a comment, but in the case of comment is ignore by the interpreter but pass is not ignored by the interpreter.
Syntax
pass
Program:
a=[1,2,3,4,5] for x in a: pass
Output:
>>> = RESTART: C:/Users/Manish/AppData/Local/Programs/Python/Python38-32/break.py >>>
Program :
def abc() pass