Nested If in Python programming | Hands on coding

Nested if statement

Syntax:

if  condition  :
   #statements
   if condition:
       #statements
       if condition:
           #statements
       
  • It is used to test the condition.
  • One if inside another if is called nested if.


/*Example 1*/
no=5
if no>2:/*true*/
      if no>3:/*true*/
        print("Hello")
    print("Hi")
/*
### Output ###
Hello
Hi
*/

/*Example 2*/
no=5
if no>2:/*true*/
      if no<3:/*false*/
        print("Hello")
    print("Hi")
/*
### Output ###
Hi
*/

/*Example 3*/
no=5
if no < 2:/*false*/
 if no > 3:/*true*/
        print("Hello")
    print("Hi")
/*
### Output ###
No output because outer if
condition is false
*/