Definition:
To check the condition in Python, if the condition is true, the code inside the if block will execute.
If the condition is not true, it goes to the next and checks the condition.
If that condition is true, then the code inside the elif block runs.
If no condition is true, then the code inside the else block runs.
Now you will clearly understand the code logic with the help of the flowchart.
If-Elif-Else Statement Flowchart :
Syntax:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true
else:
# code to execute if no conditions are true
Example:
number = 15
if number > 20:
print("Number is bigger than 20")
elif number > 10:
print("Number is bigger than 10 but less than or equal to 20")
else:
print("Number is 10 or less")
Output:
Exercise
Use the if-elif-else statement to check whether number = 15 is bigger than 20, bigger than 10, or 10 or less and print the result using Python.
