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

 

Similar post

If statement in Java language

If statement in C++ language

If statement in C language

If statement in Python language

Nested if in Java language

Nested if in C language

Nested if in C++ language

Nested if in Python language

 

Suggested for you

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

Two dim Array in C++ language

Three dim Array in C++ language

Single dim Array in Java language

Two dim Array in Java language

Three dim Array in Java language

Single dim Array in C language

Two dim Array in C language

Three dim Array in C language

 

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago