Difference between continue and pass statements in Python
Last Updated : 02 Jun, 2022
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
In this article, the main focus will be on the difference between continue and pass statement.
Continue statement
This statement is used to skip over the execution part of the loop on a certain condition. After that, it transfers the control to the beginning of the loop. Basically, it skips its following statements and continues with the next iteration of the loop.
Syntax:
continue
Pass statement
As the name suggests pass statement simply does nothing. We use pass statement to write empty loops. Pass is also used for empty control statements, functions and classes. Syntax:
pass
Difference between continue and pass
Consider the below example for better understanding the difference between continue and pass statement. Example:
Python3 # Python program to demonstrate # difference between pass and # continue statements s = "geeks" # Pass statement for i in s: if i == 'k': print('Pass executed') pass print(i) print() # Continue statement for i in s: if i == 'k': print('Continue executed') continue print(i)
Output:
g e e Pass executed k s g e e Continue executed s
In the above example, when the value of i becomes equal to 'k', the pass statement did nothing and hence the letter 'k' is also printed. Whereas in the case of continue statement, the continue statement transfers the control to the beginning of the loop, hence the letter k is not printed.
Let us see the differences in a tabular form -:
| continue | pass |
1. | The continue statement is used to reject 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 when a statement is required syntactically. |
2. | It returns the control to the beginning of the loop. | When we execute the pass statements then nothing happens. |
3. | It can be used with while loop and for loop. | It is a null Operation. |
4. | Its syntax is -: continue | Its syntax is -: pass |
5. | It is mainly used inside a condition in a loop. | The pass statement is discarded during the byte-compile phase |
Similar Reads
Difference between return and print in Python In Python, we may use the print statements to display the final output of a code on the console, whereas the return statement returns a final value of a function execution which may be used further in the code. In this article, we will learn about Python return and print statements. Return Statement
2 min read
Loops and Control Statements (continue, break and pass) in Python Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides control statements like continue, break, and pass to manage the flow of the loops efficiently. This article will explore these concepts in detail.Table of Contentfor Loopswhile LoopsControl Statemen
2 min read
Difference between end and sep in Python In this article we will discuss the difference between The end and sep are two parameters in Python's built-in print() function that will help to control how the output is formatted. end in PythonThe end is a parameter in Python's built-in print() function that controls what character(s) are printed
2 min read
Python - Difference between := and == In this article, we will be discussing the major differences between Walrus(:=) and the Comparison operator (==):= in PythonThe := operator in Python, also known as the walrus operator or assignment expression, was introduced in Python 3.8. It enables assignment and evaluation to happen simultaneous
2 min read
Difference between for loop and while loop in Python In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are 'for loop' and 'while loop'. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if w
4 min read