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 :
-
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