Nested If Statement


 Nested-If

      You can have if statements inside another if statement, this is called nested if statements. An if statement present inside another if statement which is present inside another if statement and so on. Python allows us to nest if statements within if statement means we can place an if statement inside another if statement. We use nested if statements when we need to check secondary conditions only if the first condition executes as true.

Nested if-else statement means that an if statement or if-else statement is present inside another if or if-else block.

Syntax :

if(test expression 1):

       Statement(s)                                 # executes when condition 1 is true

       if(test expression 2):

               statement(s) of nested if        # executes when condition 2 is true

               
       # nested if block is end here

# if block is end here

Flow Chart :


Example :

i=10

# first if statement 

if(i<15):
 
      print("i is smaller than 15")

      # nested if statement
   
      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






No comments:

Post a Comment