nested if

Nested if in Python programming language

Nested if in Python programming language

In this tutorial,we will discuss Nested if in Python programming language

We can have an if..elif..else statements inside another if ..elif..else…. statements.

Which is known as nested if in Python language

We will discuss this in this tutorial about Nested if in Python programming language

In the nested if, in Python, we can test a condition using an if statement after an if condition.

Nested if statements in Python

Nested if

Declaration

if(Test_condition1):#{
    #Executes when Test_condition1 is true
        if(Test_condition1): #{
               #Executes when Test_condition1 is true
          #}Innerif block is end here
#}Outer if block is end here

Flow diagram

Floe diagram-Nested if in Python

In the above the diagram, initially, the flow of control enters the outer loop and then evaluates the test expression 1. Outer loop executes only once(return true/false)

If the test expression is true, executes statements 1 and then enters the inner loop to evaluate test expression 2.

If the test expression 1 is false, the flow of control stops the execution and comes out loop body for rest.

If the test expression 2 is false, the flow of control comes to the outer loop.

Finally, outer loop returns as false and the flow of control goes to rest.

 

 

Example

age=20
marks=70
if(age>18):
    //#outer if statements
    //#if this statement is true enter inner loop
    //#if not go to rest
    if(marks>65):
        //#inner if statements
        //#if this statement is true print inner loop statement
    
        print("you are selected for college")
    
    //#if not go outer loop

When we executed above program, it will be produced following result

you are selected for college

 

Nested if else in Python

Declaration

Syntax

Syntax- Nested if in Python

Flow diagram of nested if statement in Python

Nested if in Python

How nested if works:

Initially, the flow of control enters the outer if and test expression is evaluated.

When resolving as true, it executes inside statements of if part and goes to inner if.

Then, test expression of inner if is evaluated. If it is true, the body part of inner if statements are executed. Otherwise, if it is false, the flow of control jumps out to the outer if body part to evaluate test expression.

When resolve as false(outre if), the flow of control jumps to its else part.

Example

Program 1

age=20
gpa=1.1
if age>18:
   if gpa>0.8:
      print("Your age and GPA are both enough");
      print("You have been selected for university")
   else:
      print("your gpa is not enough")
else:
   print("your age is not enough")

 

When we execute above program, it will be produced the following result

Your age and GPA  are both enough
You have been selected for university.

 

Program 2

To check if the number is a positive or negative number or zero

#a number get from user
# tis program used to check number
#is it negative or positive or Zero
number=input("Enter the number for check\n")
if number>=0: #outer if
    if number==0: #inner if
          print("number is Zero")
    else:
        print("Number is positive")
else:
      print("Number is Negative")

 

When we execute the above program, it will be produced the following result

Output 1

Enter a number: 5
Number is positive

Output 2

Enter a number: -5
Number is Negative

Output 3

Enter a number :0
number is zero

 

Suggested for you

If statement in Java

If statement in C

If statement in C++

If statement in Python

Nested if in Cpp programming language
Nested if statement in Java language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago