Break , continue and pass keywords are used in loops to add additional features for various cases.
Use to break out of the current closest enclosing loop. Often times you'll need to end a loop at a specific condition , that is where break comes in handy. Let us take an example where we make use of break statement. In the example below i have a string "helloworld" and i want to print all the characters in it upto w.
for x in "helloworld":
if x == 'w':
break
print(x)
h
e
l
l
o
The Logic flow through break statement is explained below :
continue :
continue statement helps in going to the top of closest enclosing loop. It is often used to skip the rest of body of the loop for a specific condition. In the example below , i have a string "helloworld" and i want to print all the characters in it except 'w'.
for x in "helloworld":
if x == 'w':
continue
print(x)
h
e
l
l
o
o
r
l
d
The Logic flow through continue statement is explained below :
pass :
The pass statement literally does nothing at all. It is mostly used to avoid syntax errors.
# if there is no code inside a for loop , then you will get an syntax error
pass
# pass statement can help in avoiding syntax error
The Logic flow through pass statement is explained below :
Let us see how the control flow works in a programme in which all the three keywords are used.