Skip to content
Related Articles

Related Articles

Improve Article

Difference between continue and pass statements in Python

  • Last Updated : 25 Jun, 2020
Geek Week

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.

Continue-statement-python2

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:




# 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :