Python pass Vs break Vs continue [1-1 Comparison]


Python

Python pass Vs break Vs continue Statement

In Python, break is used to exit a for loop or a while loop when certain condition is satisfied. Whereas continue statement will just by pass the current iteration and continue with the next iteration. However, pass statement is used as a place-holder statement that does nothing.

An appropriate control flow selection can be made for an application only if one is clear with the difference between pass vs break vs continue. The following example clearly shows the comparison of pass vs break vs continue.

The table below differentiates between pass Vs break Vs continue statement in Python.

break continue pass
break statement is used to bring the control out of inner most loop. continue statement is used to skip the remaining statements in the current iteration of the loop and moves the control back to the start of the loop. pass statement is used as a place-holder that does nothing.
Syntax : break Syntax : continue Syntax : pass
Brings control out of loop. Brings control to the beginning of loop Considered as a null statement. So control continues its execution in normal way.
Can be used with looping statements Can be used with the looping statements. Can be used with looping, to declare empty function and empty class.

 

Some practical examples

Example 1 - pass vs continue statement

In the example given below, we will see the difference between pass statement and continue statement.

# Initializing
print("**** pass Statement ****")
for i in range(0,10):
	if(i%2 == 0):
		pass
		print(i, " is even")
	else:
		print(i, " is odd")
print("**** continue Statement ****")		
for i in range(0,10):
	if(i%2 == 0):
		continue
		print(i, " is even")
	else:
		print(i, " is odd")

Output

**** pass Statement ****
0  is even
1  is odd
2  is even
3  is odd
4  is even
5  is odd
6  is even
7  is odd
8  is even
9  is odd
**** continue Statement ****
1  is odd
3  is odd
5  is odd
7  is odd
9  is odd

 

Example 2 - pass Vs break statement

In the example given below, we will see the difference between pass statement and break statement.


print("**** pass Statement ****")
s="Welcome to the world of python"
for i in s:
	if(i == 'p'):
		pass
		print ("\nFound character p")
	else:
		print(i, end=" ")
print("\n**** break Statement ****")		
for i in s:
	if(i == 'p'):
		break
		print("Found character p")
	else:
		print(i, end=" ")

Output

**** pass Statement ****
W e l c o m e   t o   t h e   w o r l d   o f   
Found character p
y t h o n 
**** break Statement ****
W e l c o m e   t o   t h e   w o r l d   o f  

 

Example 7 - continue Vs break statement

In the example given below, we will see the difference between continue statement and break statement.

print("**** continue Statement ****")
s="Welcome to the world of python"
for i in s:
	if(i == 'p'):
		continue
		print ("\nFound character p")
	else:
		print(i, end=" ")
print("\n**** break Statement ****")		
for i in s:
	if(i == 'p'):
		break 
		print("Found character p")
	else:
		print(i, end=" ")

Output

**** continue Statement ****
W e l c o m e   t o   t h e   w o r l d   o f   
y t h o n 
**** break Statement ****
W e l c o m e   t o   t h e   w o r l d   o f  

 

Summary

The knowledge of control flow statement and comparison between pass vs break vs continue is core to python programming language that is very useful to formulate a complex logic easily and define the structure of the program before going for actual implementation. You will frequently need to use these statements to build varied of applications. The knowledge of pass vs break vs continue in different scenarios and combinations helps solving the really complex task to a time efficient solution. In this tutorial, we covered the pass vs break vs continue statement with for loop, while loop, along with the table to demonstrate the comparison of pass vs break vs continue statement with an example. All in all, this tutorial, covers everything that you need to know in order to understand and use the pass vs break vs continue statement in appropriate scenarios in Python.

 

References

python continue and break statement
python pass statement

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment