#08 - Nested Condition

Python also provides nested conditional statements... which means a condtion within a condition .

Nested if statements means an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

SYNTAX:
if (condition1):
    # Executes when condition1 is true
    if (condition2): 
      # Executes when condition2 is true
    # inner if block ends here
# outer if block is end here

EXAMPLE :

  1. i = 10
  2. if (i == 10):
  3.   # First if statement
  4.   if (i < 15):
  5.     print ("i is smaller than 15")
  6.     # Nested - if statement
  7.     # Will only be executed if statement above
  8.     # it is true
  9.     if (i < 12):
  10.       print ("i is smaller than 12 too")
  11.   else:
  12.     print ("i is greater than 15")

OUTPUT:

i is smaller than 15
i is smaller than 12 too