Nested conditional statements
Recall that an ordinary if-else
statement has this syntax:
if <condition>:
# Indented statement block
else:
# Indented statement block
It is possible for the indented statement blocks themselves to include another if
or if-else
statement. This is called nesting, and it is both useful and common. Nesting might look like this (another if/else
within the outer if
block):
if <condition 1>:
if <condition 2>:
# Indented statement block
else:
# Indented statement block
else:
# Indented statement block
or like this (another if/else
within each of the outer if
and the outer else
blocks):
if <condition 1>:
if <condition 2>:
# Indented statement block
else:
# Indented statement block
else:
if <condition 3>:
# Indented statement block
else:
# Indented statement block
or even like this: (an if/else
within the outer if
, then another if/else
within the inner if
)
if <condition 1>:
if <condition 2>:
if <condition 3>:
# Indented statement block
else:
# Indented statement block
else:
# Indented statement block
else:
# Indented statement block
Notice that the last two cases each have three if
conditions, but they are very different from each other. The last one has three levels of nesting, while the previous one has only two levels. In python, we always pay close attention to the indentation, because it tells us which inner block is part of which outer block. That placement controls the logical flow of the program. An inner block only gets executed if the condition for its outer block evaluates to True
.
Examples of nested conditionals
Code | Output | Notes |
|
|
The condition for the outer |
|
|
The condition for the outer |
|
|
The condition for the outer |
|
|
The condition for the outer |
Exercise: For the last code block in the above examples, can you come up with initial values for x
, y
, and z
so that the output is D E
? Can you come up with initial values for x
, y
, and z
so that the output is A F
?
Putting it all together
The code below on the left checks if the user can vote in an upcoming election. It checks that the user is a US citizen, is 18 or over, and has registered to vote. The flowchart on the right shows the flow of control through the program, as it checks the three conditions.
Code | Flowchart |
|
Key points:
- Notice that this program has three levels of nesting.
- Notice that if the user enters
N
to the first question, then no further questions are asked. Similarly, if they are too young, they are not asked the last question. This is accomplished using the structure of theif-else
nesting. - The indentation is vital. Look at the code above and carefully notice how each
if
lines up directly above theelse
that pairs with it. - Copy/paste the above program into VSCode and run it. To test completely, you have to try all possible combinations of inputs, so that every possible line of code in the program gets tested. This means running the program multiple times and entering multiple possibilities at each input line. For example, what happens if the user enters
P
instead ofY
orN
? Why?