Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python
In Python, both continue
and pass
statements are used to control the flow of execution in loops and conditional statements. However, they have different purposes and behavior.
The continue
statement is used to skip the current iteration of a loop and move on to the next iteration. When the interpreter encounters the continue
statement, it immediately jumps to the next iteration of the loop, ignoring any statements that follow it in the current iteration. The loop continues with the next iteration until all iterations have been completed.
On the other hand, the pass
statement is used as a placeholder or a "do-nothing" statement. When the interpreter encounters the pass
statement, it does nothing and moves on to the next statement in the code. It is often used as a placeholder when writing code that will be filled in later.
Here's an example to illustrate the difference between continue
and pass
:
# Example using continue for i in range(1, 6): if i == 3: continue print(i) # Output: # 1 # 2 # 4 # 5 # Example using pass for i in range(1, 6): if i == 3: pass print(i) # Output: # 1 # 2 # 3 # 4 # 5
In the first example, the continue
statement is used to skip printing the number 3 in the loop. In the second example, the pass
statement is used to do nothing when i
is equal to 3, but the loop still continues to print all the numbers.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.