In case you haven’t gotten it through your head by now, if/elif/else statements are the primary mechanism we use to make decisions in our programs. The generic term for this is “conditions” because it allows us to set a condition as to whether or not some code should be executed. In these lessons, I’ve used a number of real-life decision-making examples to motivate why if-statements are useful in programming. Another real-life scenario that we can encounter when making decisions are decisions that depend on the answer to another decision that has to be made first. Here’s an example from everyday life pertaining to eating and drinking:
Notice that there are several decisions that need to be made in this flow-chart. However, some of the decisions depend on prior decisions. The question of “What should I eat?” depends on the previous question: “Am I hungry?” If you are hungry, you then ask yourself what to eat. However, if you are not hungry, we skip the eating question altogether. The question of “What should I drink?” similarly depends on the answer to the previous question.
As you’ve probably guessed, if this can happen in real-life, it can also happen in our programs. It’s not uncommon to have an if statement that depends on the condition of a previous if-statement. We can build this structure into our programs by nesting if statements inside of each-other.
Nesting if-statements is really simple - it is just putting one if (or if/elif/else chain) inside of another. The same principles of how if statements are evaluated and run in python stays exactly the same. When we nest, we are just adjusting with the structure. We can convert the previous pictoral example with food and drink into a program that helps the user make these decisions. This program would be written like so:
1 hungry = input("Are you hungry? ")
2
3 if hungry == "yes":
4 sounds_good = input("What sounds good to eat? ")
5 if sounds_good == "pizza":
6 print("Go to Pizza Hut and order pizza")
7 elif sounds_good == "burger":
8 print("Go to In-n-Out and get a burger")
9 elif sounds_good == "salad":
10 print("Go home and make a salad")
11 else:
12 print("Unsure")
13
14 thirsty = input("Are you thirsty? ")
15
16 if thirsty == "yes":
17 sounds_good = input("What sounds good to drink? ")
18 if sounds_good == "water":
19 print("Drink your water bottle")
20 elif sounds_good == "juice":
21 print("Go buy some juice")
22 else:
23 print("Unsure")
Try running this code. Notice how the chain of if / elif / else on lines 4-12 will only run if the condition on line 3 is true (in other words, if the user types “yes”). This is because we have indented the entire thing within the “outer” if statements on line 3. Everything from 4-12 will only run when the line 3 if is true. Same goes for the if on line 16. Lines 17-23 will only get reached if the condition on line 16 is true.
It’s possible to nest if-statements indefinitely, and in many combinations. How much you nest depends on how complex your conditions are. For example, the code below, though completely made-up and not very useful, is also totally valid python:
x = int(input("What is x? "))
y = int(input("What is y? "))
if x > y:
if y*2 == x:
if y < x-20:
if y == 100:
print("I reached this line!")
else:
print("Else A")
else:
print("Else B")
else:
print("Else C")
else:
print("Else D")
Though python supports an indefinite level of nesting, there can come a point where it becomes too confusing for humans to easily understand. Due to this, it is good practice to avoid nesting if-statements beyond 2 or 3 levels deep. If you find yourself writing code that exceeds this, you probably should take a step back and reconsider. Could I write this differently? Am I over-complicating this? How can this be simplified?
Whether it is deep if-statement nesting or some other complex code, these are good questions to ask yourself on a regular basis when coding, especially as the programs you write get larger and larger.