Nested IF Statement

An introduction to the use of Selection in Python

Nested IF Statement

Once you are feeling comfortable with the if, elif, and else statements, you can move on to nested conditional statements. We can use nested if statements for situations where we want to check for a secondary condition if the first condition executes as true. For this, we can have an if-else statement inside of another if-else statement. Let’s look at the syntax of a nested if statement:

if statement1:              #outer if statement
    print("true")

    if nested_statement:    #nested if statement
        print("yes")

    else                    #nested else statement
        print("no")

else:                       #outer else statement
    print("false")

A few possible outputs can result from running this code:

  • If statement1 evaluates to true, the program will then evaluate whether the nested_statement also evaluates to true. If both cases are true, the output will be:

true
yes

If, however, statement1 evaluates to true, but nested_statement evaluates to false, then the output will be:

yes
false

And if statement1 evaluates to false, the nested if-else statement will not run, so the else statement will run alone, and the output will be:

false

We can also have multiple if statements nested throughout our code:

if statement1:                  #outer if 
    print("hello world")

    if nested_statement1:       #first nested if 
        print("yes")

    elif nested_statement2:     #first nested elif
        print("maybe")

    else                        #first nested else
        print("no")

elif statement2:                #outer elif
    print("hello galaxy")

    if nested_statement3:       #second nested if
        print("yes")

    elif nested_statement4:     #second nested elif
        print("maybe")

    else                        #second nested else
        print("no")

else:                           #outer else
    statement("hello universe")

In the above code, there is a nested if statement inside each if statement in addition to the elif statement. This will allow for more options within each condition.

Let’s look at an example of nested if statements with our grade program. We can check for whether a grade is passing first (greater than or equal to 65%), then evaluate which letter grade the numerical grade should be equivalent to. If the grade is not passing, though, we do not need to run through the letter grades, and instead can have the program report that the grade is failing. Our modified code with the nested if statement will look like this:

grade = 60

if grade >= 65:
    print("Passing grade of:")

    if grade >= 90:
        print("A")

    elif grade >=80:
        print("B")

    elif grade >=70:
        print("C")

    elif grade >= 65:
        print("D")

else:
    print("Failing grade")

If we run the code with the variable grade set to the integer value 92, the first condition is met, and the program will print out Passing grade of:. Next, it will check to see if the grade is greater than or equal to 90, and since this condition is also met, it will print out A.

If we run the code with the grade variable set to 60, then the first condition is not met, so the program will skip the nested if statements and move down to the else statement, with the program printing out Failing grade.

We can of course add even more options to this, and use a second layer of nested if statements. Perhaps we will want to evaluate for grades of A+, A and A- separately. We can do so by first checking if the grade is passing, then checkingto see if the grade is 90 or above, then checkingto see if the grade is over 96 for an A+ for instance:

grade = 60

if grade >= 65:
    print("Passing grade of:")

    if grade >= 90:
        if grade > 96:
            print("A+")

        elif grade > 93 and grade <= 96:
            print("A")

        elif grade >= 90:
            print("A-")

In the code above, for a grade variable set to 96, the program will run the following:

  1. Check if the grade is greater than or equal to 65 (true)
  2. Print out Passing grade of:
  3. Check if the grade is greater than or equal to 90 (true)
  4. Check if the grade is greater than 96 (false)
  5. Check if the grade is greater than 93 and also less than or equal to 96 (true)
  6. Print A
  7. Leave these nested conditional statements and continue with remaining code
The output of the program for a grade of 96 therefore looks like this:

Passing grade of:
A

Nested if statements can provide the opportunity to add several specific levels of conditions to your code.

Introduction

Write a program that uses a one level nested IF.

Write a program that uses a two level nested IF.

Write a program that uses a three level nested IF.

9.4 Nested IF Statement Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.