Nested Conditional Statements

  • Nested Conditional Statements consist of conditional statemetns within conditional statements.
  • Flow Chart of a Nested Conditional Statement includes:
if conditionA:
    # Code here executes when 'conditionA' is True
else:
    # Code that runs when 'conditionA' is False

    if conditionB:
        # Code that runs when 'conditionA' is False
        # and 'conditionB' is True

Example of nested conditional in python:

n=int(input('Enter marks: '))

# checking the conditions
if  n>=75:
    if n >=95:
        print('Excellent')
    else:
        print('Pass')
else:
    print('Fail')
  • If Else Statements can be put inside of another if else statement using this layout for psuedo code.
if (condition 1)
    if (condition 2)
        #first block of statement
    else 
        #second block of statement
else
    if (condition 3)
        #third block of statement
    else 
        #fourth block of statement

Challenge #1: Create your own Nested Conditional, with a if/else statement inside the else code of an if/else statement.

def function(x, y, z):
    if()
    #Code here
        if()
        #Code here
        else 
        #Code here
    else
        if()
        #Code here
        else
        #Code here

Challenge #2: Create your own Nested Conditional with operators and comparing values

Ex: