Nested Conditional Statements

  • Nested Conditional Statements consist of conditional statements within conditional statements.
  • Flow Chart of a Nested Conditional Statement includes:

Format of a nested conditional in Pseudocode

IF (condition 1)
{
    <first block of statements>
}
ELSE
{
    IF (condition 2)
    {
        <second block of statements>
    }
    ELSE
    {
        <third block of statements>
    }
}

Below are some examples of nested conditionals.

age = 19
isGraduated = False
hasLicense = True

# Look if person is 18 years or older
if age >= 18:
    print("You're 18 or older. Welcome to adulthood!")

    if isGraduated:
        print('Congratulations with your graduation!')
    if hasLicense:
        print('Happy driving!')
print("What is your grade on the quiz")
grade = int(input('What is your grade on the quiz'))
if grade >= 90:
    print("Awesome grade. You do not need to make up the quiz.")
else:
    if grade >= 70:
        print("You may retake the quiz next class for up to an A.")
    else:
        print("We will review next class together and retake later.")

Now you try it!

Age = 51; 
if (Age == 30): 
    print("your old") 
else: 
    print("you should retire") 
if (Age > 50): 
    print("You have a beard") 
you should retire
You have a beard