Nested If in Python

Hello friends how are you, today in this blog i will teach you what is nested if statement, syntax of nested if , how nested if statement works and many programs using nested if in a very simple way. 

Let's start

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
*/
Here we will take three variable to store first, second and third value after taking user input of these numbers we will apply condition on it. if condition a>b and a>c are true means a is greatest value , if condition b>a and b>c are true means b is greatest value similarly if condition c>a and c>b are  true means c is greatest value.

print("Enter three number:")
a=int(input())
b=int(input())
c=int(input())
if a>b:
    if a>c:
        print(a," is greatest")
if b>a:
    if b>c:
        print(b," is greatest")
if c>a:
    if c>b:
        print(c," is greatest")
/*
### Output ###
Enter three number:
45
85
24
85  is greatest
*/


Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. Thanks.

Post a Comment

0 Comments