Loading, please wait...

nested if statements

Nested if statements means an if statement inside another if statement.

Syntax:

if (condition1):

# Executes when condition1 is true

if (condition2):

# Executes when condition2 is true

# if Block is end here

# if Block is end here

 

Example:

# python program to illustrate nested If statement

 

#!/usr/bin/python

 

i = 10

 

if (i == 10):

 

    #  First if statement

 

    if (i < 15):

 

        print ("i is smaller than 15")

 

    # Nested - if statement

 

    # Will only be executed if statement above

 

    # it is true

 

    if (i < 12):

 

        print ("i is smaller than 12 too")

 

    else:

 

        print ("i is greater than 15")

 

 

 

 

Output:

i is smaller than 15
i is smaller than 12 too