Skip to content

5.4 Nested decision statements

5.4a What are nested decision statements?

Nested decision statements are if, elif, and else statements placed inside another if, elif, or else statement. This allows for more complex decision-making by evaluating multiple conditions in a hierarchical manner.

Please refer to Section - 5.1 Decision statements if you need to revise the decision statements before you move on.

 


 

5.4b Why do we use decision statements?

We use nested decision statements to handle complex scenarios where a single condition depends on the outcome of another condition. They allow for more detailed and specific decision paths in our code. Here's a basic structure of nested decision statements:

Syntax:

if condition1:
    # Block of code executed if condition1 is true
    if condition2:
        # Block of code executed if both condition1 and condition2 are true
    else:
        # Block of code executed if condition1 is true but condition2 is false
else:
    # Block of code executed if condition1 is false

 


 

5.4c Interpret a program snippet that includes nested decision statements.

Here’s an example demonstrating nested decision statements in Python which determines the type of activity based on the weather and whether it's a weekday or weekend.

Example 5.4.1

# Example of nested decision statements
# Determine activity based on weather and day of the week

weather = "cloudy"  # Can be "sunny", "rainy", or "cloudy"
day_of_week = "weekend"  # Can be "weekday" or "weekend"

if weather == "sunny":
    print("The weather is sunny.")
    if day_of_week == "weekend":
        print("You can go to the park.")
    else:
        print("You can go for a walk after school.")
else:
    if weather == "rainy":
        print("The weather is rainy.")
        if day_of_week == "weekend":
            print("You can watch a movie at home.")
        else:
            print("You can read a book after school.")
    else:
        print("The weather is cloudy.")
        if day_of_week == "weekend":
            print("You can visit a museum.")
        else:
            print("You can do homework after school.")

Example 5.4.1 - Explanation

  1. First if statement (if weather == "sunny":)
    • Checks if the weather is "sunny".
    • If True, it prints "The weather is sunny." and checks the nested condition if day_of_week == "weekend":.
    • If day_of_week is "weekend", it prints "You can go to the park." Otherwise, it prints "You can go for a walk after school."
  2. First else statement (else:)
    • If the weather is not "sunny", it enters the else block and checks for the next condition.
  3. Nested if statement within the first else (if weather == "rainy":)
    • Checks if the weather is "rainy".
    • If True, it prints "The weather is rainy." and checks the nested condition if day_of_week == "weekend":.
    • If day_of_week is "weekend", it prints "You can watch a movie at home."
    • Otherwise, it prints "You can read a book after school."
  4. Second else statement within the first else (else:)
    • If the weather is neither "sunny" nor "rainy", it assumes the weather is "cloudy".
    • Prints "The weather is cloudy." and checks the nested condition if day_of_week == "weekend":.
    • If day_of_week is "weekend", it prints "You can visit a museum."
    • Otherwise, it prints "You can do homework after school."

 


 

Exercise 5.4.1 - Write a Python program that determines the type of discount a customer can receive based on their membership status and the amount they spend. Use nested if statements to implement the following rules:

  1. The program should have a predefined membership status which can be "none", "silver", or "gold".

  2. The program should have a predefined amount spent by the customer. e.g. amount_spent = 120

  3. If the customer has a "gold" membership:

    • If the amount spent is €100 or more, print "You get a 20% discount!"
    • Otherwise, print "You get a 15% discount!"
  4. If the customer has a "silver" membership:

    • If the amount spent is €100 or more, print "You get a 10% discount!"
    • Otherwise, print "You get a 5% discount!"
  5. If the customer has no membership ("none"):

    • If the amount spent is €100 or more, print "You get a 2% discount!"
    • Otherwise, print "You get no discount."

Exercise 5.4.1 - Model Answer - Make sure to work out the exercise before checking the model answer.