Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between `continue` and `pass` in a for loop in python?

People also ask

What is the difference between Continue break and pass in Python?

Break, Pass, and Continue statements are loop controls used in python. The break statement, as the name suggests, breaks the loop and moves the program control to the block of code after the loop (if any). The pass statement is used to do nothing.

Can you use continue in a for loop Python?

The continue statement can be used in both while and for loops.

What does pass do in a for loop?

pass simply does nothing, while continue goes on with the next loop iteration.

Can we use pass in for loop in Python?

As the name suggests pass statement simply does nothing. The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. It is like null operation, as nothing will happen is it is executed. Pass statement can also be used for writing empty loops.


Yes, they do completely different things. pass simply does nothing, while continue goes on with the next loop iteration. In your example, the difference would become apparent if you added another statement after the if: After executing pass, this further statement would be executed. After continue, it wouldn't.

>>> a = [0, 1, 2]
>>> for element in a:
...     if not element:
...         pass
...     print(element)
... 
0
1
2
>>> for element in a:
...     if not element:
...         continue
...     print(element)
... 
1
2

Yes, there is a difference. continue forces the loop to start at the next iteration while pass means "there is no code to execute here" and will continue through the remainder of the loop body.

Run these and see the difference:

for element in some_list:
    if not element:
        pass
    print(1) # will print after pass

for element in some_list:
   if not element:
       continue
   print(1) # will not print after continue

continue will jump back to the top of the loop. pass will continue processing.

if pass is at the end for the loop, the difference is negligible as the flow would just back to the top of the loop anyway.


Difference between pass and continue in a for loop:

So why pass in python?

If you want to create a empty class, method or block.

Examples:

class MyException(Exception):
    pass


try:
   1/0
 except:
   pass

without 'pass' in the above examples will throw IndentationError.


In your example, there will be no difference, since both statements appear at the end of the loop. pass is simply a placeholder, in that it does nothing (it passes execution to the next statement). continue, on the other hand, has a definite purpose: it tells the loop to continue as if it had just restarted.

for element in some_list:
    if not element:
        pass
    print element  

is very different from

for element in some_list:
    if not element:
        continue
    print element

There is a difference between them,
continue skips the loop's current iteration and executes the next iteration.
pass does nothing. It’s an empty statement placeholder.
I would rather give you an example, which will clarify this more better.

>>> for element in some_list:
...     if element == 1:
...         print "Pass executed"
...         pass
...     print element
... 
0
Pass executed
1
2

>>> for element in some_list:
...     if element == 1:
...         print "Continue executed"
...         continue
...     print element
... 
0
Continue executed
2