The main difference between Python for loop continue vs pass is pass
simply does nothing, while continue
goes on with the next loop iteration.
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.
Python for loop continue vs pass
A simple example code 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:
print('Pass')
pass
print(element)
for element in a:
if not element:
print('Continue')
continue
print(element)
Output:
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.
Pass statement
As the name suggests pass statement simply does nothing. We use Pass statements to write empty loops. Pass is also used for empty control statements, functions, and classes.
Here’s a tabular representation of the differences between continue
and pass
in Python:
Feature | continue Statement | pass Statement |
---|---|---|
Purpose | Skips the rest of the loop for the current iteration and moves to the next one. | Acts as a no-operation statement, a placeholder for doing nothing. |
Usage | Inside loops (e.g., for , while ) | Anywhere a statement is syntactically required. |
Effect | Jumps to the next iteration of the loop. | Does nothing and allows the program to continue executing the next statement. |
Example | python for i in range(5): if i == 2: continue print(i) | python for i in range(5): if i == 2: pass else: print(i) |
Comment if you have any doubts or suggestions on this Python continue vs pass topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.