Executing When One of Multiple Conditions is Met Using the elif Statement
elif
is short for else if
and it defines a code block that executes when one of multiple conditions is true.
The elif
statement is positioned between the if
and else
statements and provides additional conditions to check when the if
condition is false.
Structure of the elif Statement
You can use multiple elif
statements within the same conditional structure.
When one of the elif
conditions is true, the code in that block is executed.
As with if
and else
, every elif
line must end with a colon(:)
to mark the beginning of its code block.
Example of elif Statement
number = 5
if number > 10:
print("The number is greater than 10.")
elif number > 5:
print("The number is greater than 5 but less than or equal to 10.")
elif number > 0:
print("The number is greater than 0 and less than or equal to 5.")
else:
print("The number is 0 or negative.")
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.