Table of Contents » Chapter 3 : Processing : Decisions : if-elif-else Statements
if-elif-else Statements
Contents
- Overview
- General Form
- Example
- Practice Problems
Overview
So far we have learned two decisions structures, the if and the if-else. Now we'll consider the if-elif-else which allows us to chain numerous decision conditions into one decision structure. In programming, we often need to decide from more than just two possible options. For example, if we need to write a program that determines students' letter grades based on their scores, a True/False decision structure (if-else) will not suffice. This is where the if-elif-else structure comes in.
The general form of the if-elif-else statement looks like this:
if condition_1:
code_block_based_on_condition_1
elif condition_2:
code_block_based_on_condition_2
elif condition_3:
code_block_based_on_condition_3
.
.
.
elif condition_n:
code_block_based_on_condition_n
else
code_block_based_on_else
The key details to notice about this general form are the following:
- The initial if keyword which indicates to Python that it must evaluate an expression (condition_1) in order to make a decision.
- The condition_1 that follows the if keyword is the expression that must evaluate to True in order for the code_block_based_on_condition_1 to be run. If the condition evaluates to False, the elif condition (condition_2) is evaluated. If condition_2 is True then code_block_based_on_condition_2) after the elif: will be run instead.
- If condition_2 evaluates to False, then then elif condition_3 is evaluated. If condition_3 is True, then code_block_based_on_condition_3) after the elif: will be run instead.
- The progression each successive condition continues until one of the conditions evaluate to True.
- If none of the conditions are True, then the else code block will run. If there is no else included in the code, then the if statement will exit with none of the code blocks being run.
- The colons (:) at the end of the if statement, the elif's and the else statement are required syntactic element that indicates the end of those statements and the beginning of their associated code blocks.
- The indentation of the code blocks is a requirement in Python that indicates what line(s) of code are included in the code block.
- The code blocks can be from one to many lines of code.
Figure 1 depicts the general form of the if-elif-else statement. The execution flow represents the flow of the program up to the point of the if-elif-else condition:. When the if condition is evaluated by the interpreter, the result will be either True or False. If the result is True then execution flow moves into the code_block_based_on_condition_1 and that code runs, then execution flow drops down to the code after the if statement. If the condition result is False then execution flow moves into the next condition, which is evaluated the same way as Condition 1. Each subsequent condition is represented the same way. If the results of all conditions are False, then the last code block based on the else portion of this statement executes.
Here's a code example of the if-elif-else statement:
message = "The month's name is "
print("Program Started.")
month_number = int(input("Please enter the month number (1 to 12): "))
if month_number == 1:
message = message + "January"
elif month_number == 2:
message = message + "February"
elif month_number == 3:
message = message + "March"
elif month_number == 4:
message = message + "April"
elif month_number == 5:
message = message + "May"
elif month_number == 6:
message = message + "June"
elif month_number == 7:
message = message + "July"
elif month_number == 8:
message = message + "August"
elif month_number == 9:
message = message + "September"
elif month_number == 10:
message = message + "October"
elif month_number == 11:
message = message + "November"
elif month_number == 12:
message = message + "December"
else:
message = "Invalid Month Number"
print(message)
print("Program completed.")
And here are two sample runs of the program, one with a valid entry and one with an invalid entry:
Program Started.
Please enter the month number (1 to 12): 5
The month name is May
Program completed.
Program Started.
Please enter the month number (1 to 12): 55
Invalid Month Number
Program completed.

For these problems, you may need to refer to the Comparison Operators and Logical Operators tables on the previous if statement page. You may also need the table operators on the Basic Operators page as well. These tables are also available in the Reference Materials.
Problem 1
if-elif-else Statement: Write a Python program that prompts the user for an integer and then prints whether that number is positive, negative, or zero.
Please enter an integer: -5
Your integer is negative.
Problem 2
if-elif-else Statement: Write a Python program that prompts the user for two integers and then prints their relationship, that is, is the first number larger than the second, equal to the second, or smaller than the second?
--------------------------------------------------
This program will prompt you for two integers,
then it will tell you their relationship:
--------------------------------------------------
Please enter your first integer: 10
Please enter your second integer: 20
Your first integer is smaller than your second integer.