A 'nested if else' is an if statement in python is made by two word either if or else.
In python programming a situation occurs where we have to check more conditions. Nested if-else is a statement inside another if-else statement.
In simple word we can define a if statement inside it another if-else statement is known as nested if statement. We can use one if or else if statement inside another if or elif statements.
Nested if else Statement in Python
Syntax : if elif else(nested) statement in python
# Outer if condition
if condition1:
# inner if condition
if condition2:
#Block of Statements1
# inner else condition
else :
#Block of Statements 2
#Outer else statement
else:
# inner if condition
if condition3:
# Block of Statements 3
#inner else condition
else:
#Block of Statements 4
Explanation: Here, the given above syntax is for nested if else in python. if the given outer condition1 evaluate true, then control will jump to inner (nested)if condition 2.
If inner condition2 evaluates true, then it will proceed to "#Block of statement 1" otherwise "#Block of statement 2" will proceed.
Similarly, in outer else part will execute.
In above given syntax, first python will check for outer if condition1.
If outer if condition1 is true, then it will check for inner(nested) if statement. If outer if condition1 evaluates false, then control will move to outer else part and so on.
Here if condition3 evaluates true, then control will proceed to execute "#Block of statement 3".
Otherwise "#Block of Statements 4"
executes.
Given fig. show how nested if else statement works in python.
Python starts script execution from top and goes to end.
After step by step successful execution of script the control
proceeds or test condition1. if the given condition1 is true then control jumps to execute condition2 again here it check if condition2 is true or false. if condition2 is true then executes statement 1 else execute statemet 2 and program stop the execution.
When given condition1 evaluates false then control proceed to execute condition3.If the given condition3 is true then it proceed statement3 otherwise statement 4 will be executed and finally stop the execution.
Example 1: Python script to check given number is positive,negative or zero.
p=int(input("Enter Any Number"))
if p>0: #test-condition
print(p," is Positive Number. ")
else :
if p<0:
print(p," is Negative Number.")
else:
print("The Given number is Zero ")
Output:
8 is Positive Number.
The above program illustrates the use of nested if else statements to check Given number is postive,negative or zero.
1. In the above script, we have initialized variable p with value as 8
2. Then, we have used if p>0: with a test-expression to check the given number is small than 0 . We have used a relational operator > in if statement. Since the value of p is greater than 0, the condition becomes true and execute execute code inside body of if statement i.e. print(p," is Positive Number. ") which gives output 8 is Positive Number.
3. Let suppose the value for p is zero i.e. p=0, the condition if p>0: evaluates to false and control jumps to else statement.
inside else the python interpreter again test the condition if p<0: which is again false, and finally control proceed to execute statements inside else i.e.
print("The Given number is Zero ")
After that, the control goes outside of the block and script terminates with a successful result.
Previous Topic:-->> if elif else statement in Python || Next topic:-->>short hand if else in Python.