5.9. Chained Conditionals¶
A chained conditional consists of a series of checks that are evaluated one
after the other. If one check in the series evaluates to True, then all of
the following checks are ignored.
5.9.1. elif Statements¶
If/else statements provide two alternative paths. A single condition
determines which path to follow. We can build more complex conditionals using
an elif clause. elif stands for else if, and it allows us to include
more conditions and code blocks. This leads to more branches for our code to
follow.
Example
1 2 3 4 5 6 7 8 9 | num = 10
other_num = 20
if num > other_num:
print(num, "is greater than", other_num)
elif num < other_num:
print(num, "is less than", other_num)
else:
print(num, "is equal to", other_num)
|
Console Output
10 is less than 20
Summary:
Line 4 begins the chained conditional. The boolean expression
num > other_numreturnsFalse, since 10 is not greater than 20. This causes line 5 to be skipped.Line 6 contains the
elifstatement. The boolean expressionnum < other_numreturnsTrue, since 10 is less than 20. This triggers line 7.The code block for the
elseclause (line 9) is skipped, because one of the conditions above it was true.
Note
Just like with a simple if statement, the else clause is also optional
for elif statements. In the example above, removing lines 8 & 9 will
not break anything. However, the program will not print anything when
num equals other_num, since the if and elif conditions both
return False.
5.9.1.1. Multiple elif Statements¶
We can include more than one elif statement within a conditional. For
example, the following code sample prints different messages depending on the
first character in a string.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 | character = 'P'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
if character in lowercase:
print(character, 'is a lowercase letter.')
elif character in uppercase:
print(character, 'is an UPPERCASE letter.')
elif character in digits:
print(character, 'is a number.')
else:
print('&**%#!', character, 'is a punctuation mark or a space.')
|
We can easily add more elif statements to the conditional if we need to
perform more checks. This gives us a huge amount of flexibility if we decide
to modify the program later.
Here are some rules for building if/elif/else statements:
Order is important. The
ifstatement comes first, then theelifstatements, and finally theelse.The
ifstatement and code block are required. Theelifandelseclauses are optional.Conditionals contain only ONE
ifstatement and only ONEelse(if used).Multiple
elifstatements are allowed after theifstatement, but they must come before theelseclause.
No matter how long we make a chained conditional, no more than one of the code blocks will run.
Example
1 2 3 4 5 6 7 8 9 10 11 | num = 10;
other_num = 20;
if num > other_num:
print(num, "is greater than", other_num)
elif num < other_num:
print(num, "is less than", other_num)
elif num % 5 == 0:
print(num, "is divisible by 5")
elif num % 2 == 0:
print(num, "is even")
|
Console Output
10 is less than 20
Even though both of the conditions num % 5 == 0 and num % 2 == 0
evaluate to True, neither line 9 nor 11 runs. Since line 6 is satisfied
first, the rest of the conditional is skipped.
5.9.2. Nested vs. Chained Conditionals¶
On the previous page, we used a nested conditional to print different outputs based on the length of a word. We can accomplish the same result with a chained conditional.
Example
Here is the nested conditional again:
1 2 3 4 5 6 7 8 9 | word = input('Please enter a word: ')
if len(word) == 4:
print("What did your mom tell you about using 4-letter words?")
else:
if len(word) < 4:
print("You can think of a longer word than that!")
else:
print("Excellent word!")
|
The following chained conditional produces the same result:
1 2 3 4 5 6 7 8 | word = input('Please enter a word: ')
if len(word) == 4:
print("What did your mom tell you about using 4-letter words?")
elif len(word) < 4:
print("You can think of a longer word than that!")
else:
print("Excellent word!")
|
Often, you can use a nested conditional or a chained conditional to solve the same problem. Which one you choose depends on your personal preference, but you should always use the option that makes your code easier for others to read.
Example
Nesting one conditional inside of another performs a check within a check, and we can do this any number of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | if condition_1:
# code here
if condition_2:
# code here
if condition_3:
# code here
else:
# code here
else:
# code here
else:
# code here
|
Perhaps you see the problem with nesting more than once or twice. The code quickly gets very difficult to read and follow.
Even though we COULD code a check within a check within a check within a check within a check (etc.), we really SHOULDN’T. In most instances, we can make our code more readable by using chained conditionals and/or logical operators in place of nested conditionals.
5.9.3. Check Your Understanding¶
Question
What does the following code print?
1 2 3 4 5 6 7 8 | num = 8
if num % 2 == 0:
print("Launch")
elif num > 5:
print("Code")
else:
print("LaunchCode")
|
-
Launch -
Code -
Launch
Code -
LaunchCode
Question
Examine this nested conditional:
1 2 3 4 5 6 7 8 | num = -10
if num < 0:
print("The negative number", num, "is not valid here.")
else:
if num > 0:
print(num, "is a positive number")
else:
print(num, "is 0")
|
Which of the following code blocks gives the same result?
2 3 4 5 6 7
if num < 0: print("The negative number", num, "is not valid here.") else num > 0: print(num, "is a positive number") else: print(num, "is 0")
2 3 4 5 6 7
if num < 0: print("The negative number", num, "is not valid here.") elif num > 0: print(num, "is a positive number") else: print(num, "is 0")
2 3 4 5 6 7
if num < 0: print("The negative number", num, "is not valid here.") if num > 0: print(num, "is a positive number") else: print(num, "is 0")
- Code sample a
- Code sample b
- Code sample c
