Continue and Pass Statements in Python
Difference between continue and pass statements in Python
The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we have to skip the iteration, and can ignore the condition due to any circumstances. This process can be done using loop control statements. The execution can be changed from its normal flow by loop control statements. When a scope is left by the execution, all automatic objects are destroyed that were created in the scope.
Continue statement
The continue statement is used for skipping over the loop execution part on a specific condition. Then the control is transferred to the starting of the loop. Its resulting statements are skipped and continue with the next iteration of the loop.
Syntax:
Continue
Pass statement
The pass statement is doing nothing as the name implies. The empty loops are written by using pass statements. we can use the pass statement for empty control statements, functions, and classes.
Syntax:
Pass
Difference between continue and pass
Here, we are demonstrating the difference between the continue and pass statements with the use of examples.
Example:
# Python program for demonstrating
# the difference between pass and
# continue statement
s = "Tutorial"
# Pass statement
for i in s:
if i == 'a':
print('Pass executed')
pass
print(i)
print()
# Continue statement
for i in s:
if i == 'a':
print('Continue executed')
continue
print(i)
Output:
T
u
t
o
r
i
Pass executed
a
l
Continue executed
Explanation: In the above program, we have defined the difference between a pass and continue statement. And you can see the displayed result of both statements.
Conclusion
In the above article, we have discussed pass and continue statements. Also, we have seen the difference between both the pass and continue statements.