Break , Continue , Pass

Break , continue and pass keywords are used in loops to add additional features for various cases.

break:

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)

output :
h  e  l  l  o

The Logic flow through break statement is explained below :

flow of logic through the break statement.
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)

output :
h  e  l  l  o  o  r  l  d

The Logic flow through continue statement is explained below :

flow of logic through continue statement.
pass :

The pass statement literally does nothing at all. It is mostly used to avoid syntax errors.

for x in “hello” :
# if there is no code inside a for loop , then you will get an syntax error
for x in “hello” :
pass
# pass statement can help in avoiding syntax error

The Logic flow through pass statement is explained below :

flow of logic through pass statement.

Let us see how the control flow works in a programme in which all the three keywords are used.


flow of logic break,continue and pass.