Subscribe Contact

Home  »  Chapter 3 : Decisions
if-elif-else Statements

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.

General Form

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
Notice the indentation. The lines of code in the code blocks for the if and all subsequent elif's are indented so the interpreter knows what lines of code are included in those blocks.

Example

The flowchart shown here represents the general form of the if-elif-else statement as well. 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.

Practice Problems

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.




 


«  Previous : Decisions : if-else Statements
Next : Decisions : In-line if Statements  »



© 2023 John Gordon
Cascade Street Publishing, LLC