Python Continue in if statement used to skip current iteration and instructs a loop to continue to the next iteration. Use continue statements within loops, usually after an if statement.
Continue in the if statement Python
A simple example code uses a continue statement in Python to skip over part of a loop when a condition is met. When bb is equal to 2, Skip prints the second name and then continues iterating.
bb = ["Walter", "White", "Jesse", "Pinkman"]
for cast in range(0, len(bb)):
if cast == 2:
continue
else:
print(bb[cast])
print("Counter is ",cast)
print("Program Complete")
Output:
You can include multiple conditions using elif
(short for “else if”) and a final catch-all condition using else
. Here’s a basic structure:
if condition1:
# code to execute if condition1 is True
elif condition2:
# code to execute if condition2 is True
elif condition3:
# code to execute if condition3 is True
else:
# code to execute if none of the above conditions are True
You can continue an if
statement by adding more conditions using elif
or adding more code blocks within the existing conditions. Here’s an example:
x = 10
if x > 10:
print("x is greater than 10")
elif x < 10:
print("x is less than 10")
else:
print("x is equal to 10")
# You can continue with more code outside the if statement
print("This statement will always be executed")
Remember, in Python, the indentation level determines the scope of the code blocks. All statements indented under the same level are considered part of the same block.
Comment if you have any doubts or suggestions on this Python continue 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.