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.
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
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
Syntax
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 Python language
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java 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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…