Difference Between pass and continue Keywords in Python

Difference Between pass and continue Keywords in Python

Python has some reserved words known as keywords that the Python interpreter recognizes.

None, return, for, try, while, break, pass, and continue are some of the keywords found in the Python programming language. Interestingly, some keywords are primarily used in standard settings, so their purpose is mistaken.

For example, break and continue are mostly used inside if and else statements and inside for loops and while loops. return is used inside functions, and sometimes, we can also find the pass keyword.

One such pair of keywords is pass and continue. They are found inside loops and conditional statements. Their behavior is sometimes mistaken to be the same.

This article will discuss the difference between pass and continue keywords in Python.

Difference Between pass and continue Keywords in Python

The pass keyword in Python is a null statement. When a Python interpreter lands on this statement, it parses it, but nothing happens.

Generally, developers and programmers use it as a placeholder for code they plan to write in the near future.

Many people think that the pass statement is ignored by a Python interpreter, like comments (statements starting with an #), but that is not true. A Python interpreter knows that no operation must be performed for the pass statement.

The continue keyword or statement stops the execution of the following code for an iteration. Any code that follows the continue statement does not get executed. A Python interpreter jumps to the next iteration.

The continue statement is used when a programmer or a developer wishes to perform no action for a blocklisted condition.

Let us understand these two statements with the help of some examples. Refer to the following Python code for the pass statement.

for i in range(10):
    if i % 2 == 0:
        pass

    print(i)

Output:

0
1
2
3
4
5
6
7
8
9

From the output, we can understand that the iteration number will get printed even after the pass statement is present inside the if statement. As mentioned above, a Python interpreter will perform no action when it encounters a pass statement.

Refer to the following Python code for the continue statement.

for i in range(10):
    if i % 2 == 0:
        continue

    print(i)

Output:

1
3
5
7
9

From the output, we can infer that the code after the continue statement, no matter if it is inside the same conditional statement block or not, will strictly not get executed. A Python interpreter will shift to the next iteration after discovering the continue statement.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub