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.