Skip to content

Python for loop continue vs pass

  • by

The main difference between Python for loop continue vs pass is pass simply does nothing, while continue goes on with the next loop iteration.

Python for loop continue and pass

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:

Python for loop continue vs pass

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:

Featurecontinue Statementpass Statement
PurposeSkips 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.
UsageInside loops (e.g., for, while)Anywhere a statement is syntactically required.
EffectJumps to the next iteration of the loop.Does nothing and allows the program to continue executing the next statement.
Examplepython 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.

Leave a Reply

Your email address will not be published. Required fields are marked *