Summary: In this tutorial, we will learn about jump statements in Python such as break, continue, and pass statements.
Introduction to Python Jump Statement
Let’s discuss with examples about each statements.
Python Break Statement
Break statement in python is used to exit from the loop when a certain condition is satisfied.
It is often used to terminate the execution or iteration of ‘for’ or ‘while’ loop.
Here is an example in python which uses a
Recommended: Python range()
x = 0 for x in range(10): if x == 4: break print(x)
In the above python program, we first initialized
x=0 then we constructed a ‘for statement‘ to output all numbers from 0 to 9.
for loop, there is also a
break statement with condition
x==4. This means the loop will terminate when
x value gets equal to
In the output, we got what we expected i.e. the execution of the loop stops at
4 and as a result, we didn’t get any number greater than or equal to
4 in the output.
Python Continue Statement
Continue statement in python is used to skip the part of a ‘for’ or ‘while’ loop whenever a condition gets satisfied.
We usually use it when we don’t want to terminate the loop but skip some iterations in between depending on some conditions.
Let’s see an python example of a
continue statement for better intuition.
x = 0 for x in range(10): if x == 4: continue print(x)
In the above program, we have written a ‘for loop’ to print all numbers from 0 to 9.
But within the loop, there is also an if statement with condition
x==4 along with
This means when
continue statement forces the loop to go for its next iteration rather than executing the statements which follows (i.e
That is why we didn’t get 4 in the output.
The difference between Python break and continue is that the break statement stops the execution of the loop whereas continue does not.
Python Pass Statement
Pass statement in python is used as a placeholder for implementations inside functions, loops, etc.
It allows bypassing the syntactical requirement i.e the code blocks such as def, if, class, etc cannot be empty.
Let’s see an example using a
x = 0 for x in range(10): if x == 4: pass print(x)
Notice the output is as there is no conditional statement in the program. In short,
pass didn’t have any effect on the program end result.
pass statement inside the loop as a placeholder for future implementations. Use it when you don’t have any statement as of now to write inside
if statement but is sure that in the future you will surely have the implementation.
I hope now you understood the significance of
pass statements in Python and theirs difference as well.
In this tutorial, we learned the use of Python break, Python continue and Python pass statement with examples.