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.
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
| |
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.
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
| |
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
| |
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.
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:
| |
The following chained conditional produces the same result:
| |
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.
| |
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.
Check Your Understanding
Question
What does the following code print?
| |
LaunchCodeLaunch CodeLaunchCode
Question
Examine this nested conditional:
| |
Which of the following code blocks gives the same result?
1 2 3 4 5 6if 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")1 2 3 4 5 6if 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")1 2 3 4 5 6if 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")