Chapter 19  Nested Selection Structures
 Authors
 Ms. Samavi Salman
 Dr. Rao Muhammad Adeel Nawab
 Supporting Material
Nested IF in Python
 Nested IF
 Definition
 if statements inside if statements, this is called nested if statements
 Purpose
 The main purpose of Nested if statements are often used when you must test a combination of conditions before deciding on the proper action
 Importance
 Nestedif statement is also the most important and easiest concept of control flow statements. This statement is very helpful, while or when more than one condition needs to be checked based on the problem
 Situation to Use
 There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct
 Flowchart – Nested IF Statement
 Syntax – Nested IF
'''
if Condition is True the block will be executed otherwise the control will move to the endif statement
'''
if condition:
if condition:
….
endif
endif
 Syntax – IF  ELIF  ELSE Statement
'''
if Condition is True the block will be executed otherwise the control will move to the elif statement if the elif condition is False the else block will be executed.
'''
if condition:
….
elif condition:
….
else:
….
endif
 Flowchart – IF  ELIF  ELSE Statement
 Example 1 – IF  ELSE Statement
Completely and Correctly Understand a Task 
Task Description 

Sample Outputs 
Sample Output 01 Enter an Integer Number: 100 100 is a Positive Number ———————————————– Sample Output 02 Enter an Integer Number: 100 1 is a Negative Number ———————————————– Sample Output 03 Enter an Integer Number: 0 Number is ZERO 
Plan and Design Solution to the Task 
InputProcessingOutput 

Pseudo Code 
Step 1: Start Step 2: Input – number (Integer Value) Step 3: if number is greater than 0 Print – Positive Number Step 5: elif number is less than 0 Print – Negative Number Step 6: else Print – ZERO endif Step 7: Stop 
Flow Chart 
Implementation in Python 
Code 
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take an Integer Number as Input from User and check whether it’s a Positive Number or a Negative Number?
'''
try:
# Take an Integer Number as Input from User
number = int(input("Enter an Integer Number: "))
# Decision Making  Check whether Condition is True or False
if number > 0:
print(number, "is a Positive Number")
elif number < 0:
print(number, "is Negative Number")
else:
print("Number is ZERO")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer Number")
Execution of Code (Dry Run)  
Python Statement  Calculations  Variable(s)  Output Screen 
number = int(input(“Enter an Integer Number: “))  number = 100  Enter an Integer Number: 100  
if number > 0:  Decision Making – Check Whether Condition is True or False number > 0 100 > 0 False (i.e., Statement(s) after ifStatement will not be executed)  number = 100  Enter an Integer Number: 100 
print(number, “is a Positive Number”)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number = 100  Enter an Integer Number: 100 
elif number < 0:  Decision Making – Check Whether Condition is True or False number < 0 100 < 0 True (i.e., Statement(s) after ifStatement will be executed)  number = 100  Enter an Integer Number: 100 
print(number, “is a Negative Number”)  This Statement will be executed because Condition is True in above ifStatement (see Output Screen)  number = 100  Enter an Integer Number: 100 100 is a Negative Number 
Output of Code  
Enter an Integer Number: 100 100 is a Negative Number 
 Example 2 – IF  ELSE Statement
Completely and Correctly Understand a Task 
Task Description 

Sample Outputs 
Sample Output 01 Enter First Number: 5.2 Enter Second Number: 3 Enter Operator (+, , *, /, **): * 5.2 * 3 = 15.6 —————————————— Sample Output 02 Enter First Number: 10 Enter Second Number: 3 Enter Operator (+, , *, /, **): ** 10 ** 3 = 1000 —————————————— Sample Output 03 Enter First Number: 4.35 Enter Second Number: 3.25 Enter Operator (+, , *, /, **): / 4.35 / 3.25 = 1.33 —————————————— Sample Output 04 Enter First Number: 10 Enter Second Number: 30 Enter Operator (+, , *, /, **): – 10 – 30 = 20 —————————————— Sample Output 05 Enter First Number: 100 Enter Second Number: 500 Enter Operator (+, , *, /, **): + 100 + 500 = 600 
Plan and Design Solution to the Task 
InputProcessingOutput 

Pseudo Code 
Step 1: Start Step 2: Input – number1 Step 3: Input – number2 Step 4: Input – operator (Arithmetic Operator) Step 5: if operator == ‘+’ Print – Sum of number1 and number2 Step 6: elif operator == ‘‘ Print – Subtraction of number2 from number1 Step 7: elif operator == ‘*’ Print – Product (Multiplication) of number1 and number2 Step 8: elif operator == ‘/’ Print – Division of number1 by number2 Step 9: elif operator == ‘**’ Print – Exponent of number1 with number2 endif Step 10: Stop 
Flow Chart 
Implementation in Python 
Code 
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers and an Operator as Input from User and perform Arithmetic Operation on Operands (Two Numbers) based on Value of Operator
'''
try:
# Take Two Numbers (Integers or Float) as Input from User
number1 = float(input("Enter First Number: "))
number2 = float(input("Enter Second Number: "))
# Take Operator as input from User
operator = input("Enter an Operator: ")
# Decision Making – Check whether Value of Operator is + or not
if operator == '+':
print(number1,"+",number2,"=", number1 + number2)
elif operator == '':
print(number1,"",number2,"=", number1  number2)
elif operator == '*':
print(number1,"*",number2,"=", number1 * number2)
elif operator == '/':
print(number1,"/",number2,"=", number1 / number2)
elif operator == '**':
print(number1,"**",number2,"=", umber1 ** number2)
else:
print("Invalid Operator!")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer Number")
except ZeroDivisionError:
print("Sorry !!!. Invalid Division. Please enter Integer Number other than ZERO")
Execution of Code  
Python Statement  Calculations  Variable(s)  Output Screen 
number1 = float(input(“Enter First Number: “))  number1 = 5  Enter First Number: 5  
number2 = float(input(“Enter Second Number: “))  number1 = 5 number2 = 2  Enter First Number: 5 Enter Second Number: 2  
operator = input(“Enter an Operator: “)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: **  
if operator == ‘+’:  Decision Making – Check Whether Condition is True or False operator == + ** == + False (i.e., Statement(s) after ifStatement will not be executed)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
print(number1,”+”,number2,”=”, number1 + number2)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
elif operator == ‘‘:  Decision Making – Check Whether Condition is True or False ** == – False (i.e., Statement(s) after ifStatement will not be executed)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
print(number1,”“,number2,”=”, number1 – number2)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
elif operator == ‘*’:  Decision Making – Check Whether Condition is True or False ** == * False (i.e., Statement(s) after ifStatement will not be executed)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
print(number1,”*”,number2,”=”, number1 * number2)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
elif operator == ‘/’:  Decision Making – Check Whether Condition is True or False ** == / False (i.e., Statement(s) after ifStatement will not be executed)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
print(number1,”/”,number2,”=”, number1 / number2)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 5 number2 = 2 operator = **  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
elif operator == ‘**’:  Decision Making – Check Whether Condition is True or False ** == ** True (i.e., Statement(s) will be executed because the previous condition is true)  number1 = 5 number2 = 2 operator = ** 5 ** 2 = 25  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 
print(number1,”**”,number2,”=”, number1 ** number2)  This Statement will be executed because Condition is True in above elifStatement (see Output Screen)  number1 = 5 number2 = 2 operator = ** 5 ** 2 = 25  Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 5 ** 2 = 25 
Output of Code  
Enter First Number: 10 Enter Second Number: 30 Enter an Operator: / 10 / 30 = 0.33 
 Example 3 – IF  ELSE Statement
Completely and Correctly Understand a Task 
Task Description 

Sample Outputs 
Sample Output 01 Enter an Integer Number: 5000 5000 is a Positive Number And 5000 is an Even Number ——————————————————— Sample Output 02 Enter an Integer Number: 7000 5000 is a Positive Number And 7000 is an Odd Number ——————————————————— Sample Output 03 Enter an Integer Number: 0 ZERO is a Neutral Number ——————————————————— Sample Output 04 Enter an Integer Number: 50.45 Sorry !!!. Invalid Input. Please enter Integer Number ——————————————————— Sample Output 05 Enter an Integer Number: ambition Sorry !!!. Invalid Input. Please enter Integer Number 
Plan and Design Solution to the Task 
InputProcessingOutput 

Pseudo Code 
Step 1: Start Step 2: Input – number (Integer Value) Step 3: if number is greater than 0 Print – Positive Number If number % 2 == 0 Print – Even Number else Print – Odd Number elif number is equal to 0 Print – ZERO else if number % 2 == 0 Print – Even Number else Print – Odd Number endif Step 4: Stop 
Flow Chart 
Implementation in Python 
Code 
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take an Integer Number as Input from User and check whether it’s a Positive Number or a Negative Number also that whether it’s an Even number, Odd number or ZERO?
'''
# Take an Integer Number as Input from User
number = int(input("Enter an Integer Number: "))
# Decision Making  Check whether Condition is True or False
if number > 0:
print(number, "is a Positive Number")
if number % 2 == 0:
print("And", number, "is an Even Number")
else:
print(number, "is an Odd Number")
# Decision Making  Check whether Condition is True or False
elif number == 0:
print("ZERO is a Neutral Number")
else:
print(number, "is a Negative Number")
if number % 2 == 0:
print(number, "is an Even Number")
else:
print("And", number, "is an Odd Number")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer Number")
Execution of Code (Dry Run)  
Python Statement  Calculations  Variable(s)  Output Screen 
try:  
number = int(input(“Enter an Integer Number: “))  number = 3  Enter an Integer Number: 3  
if number > 0:  Decision Making – Check Whether Condition is True or False number > 0 3 > 0 False (i.e., Statement(s) will be executed after ifStatement)  number = 3  Enter an Integer Number: 3 
print(number, “is a Positive Number”)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 
if number % 2 == 0:  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 
print(“And”, number, “is an Even Number”)  number = 3  Enter an Integer Number: 3  
else:  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 
print(number, “is an Odd Number”)  number = 3  Enter an Integer Number: 3  
elif number == 0:  Decision Making – Check Whether Condition is True or False 3 == 0 3 < 0 False (i.e., Statement(s) will be executed after ifStatement)  number = 3  Enter an Integer Number: 3 
print(“ZERO is a Neutral Number”)  This Statement will not be executed because Condition is False in above elifStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 
else:  number = 3  Enter an Integer Number: 3  
print(number, “is a Negative Number”)  This Statement will be executed because Condition is True in above elseStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 3 is a Negative Number 
if number % 2 == 0:  Decision Making – Check Whether Condition is True or False 3 % 2 == 0 False (i.e., Statement(s) will be executed after ifStatement)  number = 3  Enter an Integer Number: 3 3 is a Negative Number 
print(number, “is an Even Number”)  This Statement will not be executed because Condition is False in above elifStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 3 is a Negative Number 
else:  number = 3  Enter an Integer Number: 3 3 is a Negative Number  
print(“And”, number, “is an Odd Number”)  This Statement will be executed because Condition is True in above elseStatement (see Output Screen)  number = 3  Enter an Integer Number: 3 3 is a Negative Number And 3 is an Odd Number 
except ValueError:  
print(“Sorry !!!. Invalid Input. Please enter Integer Number”)  
Output of Code  
Enter an Integer Number: 3 3 is a Negative Number And 3 is an Odd Number 
 Example 4 – IF  ELSE Statement
Completely and Correctly Understand a Task 
Task Description 

Sample Outputs 
Sample Output 01 Enter First Number: 1200 Enter Second Number: 1000 Enter Third Number: 1100 number1 is greater than number3 number1 is greater than number2 ——————————————————— Sample Output 02 Enter First Number: 1000 Enter Second Number: 1200 Enter Third Number: 1100 number2 is greater than number1 number2 is greater than number3 ——————————————————— Sample Output 03 Enter First Number: 1100 Enter Second Number: 1000 Enter Third Number: 1200 number1 is less than number3 number1 is greater than number2 ——————————————————— Sample Output 04 Enter First Number: 1000 Enter Second Number: 1000 Enter Third Number: 1000 number1,number2 and number3 are equal ——————————————————— Sample Output 05 Enter First Number: 12.0 Enter Second Number: 10.0 Enter Third Number: 11.0 number1 is greater than number3 number1 is greater than number2 ——————————————————— Sample Output 06 Enter First Number: 10.0 Enter Second Number: 12.0 Enter Third Number: 11.0 number2 is greater than number1 number2 is greater than number3 ——————————————————— Sample Output 07 Enter First Number: 11.0 Enter Second Number: 10.0 Enter Third Number: 12.0 number1 is less than number3 number1 is greater than number2 ——————————————————— Sample Output 08 Enter First Number: 10.0 Enter Second Number: 10.0 Enter Third Number: 10.0 number1,number2 and number3 are equal ——————————————————— Sample Output 09 Enter First Number: hardwork Sorry Invalid Input !!! Please enter Integer / Float Number 
Plan and Design Solution to the Task 
InputProcessingOutput 

Pseudo Code 
Step 1: Start Step 2: Input – number1 (Integer Value) Step 3: Input – number2 (Integer Value) Step 4: Input – number3 (Integer Value) Step 5: if number1 > number2 if number1 > number3 print(“number1 is greater than number3”) else: print(“number1 is less than number3”) print(“number1 is greater than number2”) elif number2 > number3 if number2 > number1 print(“number2 is greater than number1”) else: print(“number2 is less than number1”) print(“number2 is greater than number3”) elif number3 > number1 if number3 > number2 print(“number3 is greater than number2”) else: print(“number3 is less than number2”) print(“number3 is greater than number1”) else print(“number1,number2 and number3 are equal”) endif Step 6: Stop 
Flow Chart 
Implementation in Python 
Code 
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take three Numbers as Input from User and check which number is Greater?
'''
try:
# Take a Number as Input from User
number1 = float(input("Enter First Number: "))
number2 = float(input("Enter Second Number: "))
number3 = float(input("Enter Third Number: "))
# Decision Making  Check whether Condition is True or False
if number1 > number2:
if number1 > number3:
print("number1 is greater than number3")
else:
print("number1 is less than number3")
print("number1 is greater than number2")
# Decision Making  Check whether Condition is True or False
elif number2 > number3:
if number2 > number1:
print("number2 is greater than number1")
else:
print("number2 is less than number1")
print("number2 is greater than number3")
# Decision Making  Check whether Condition is True or False
elif number3 > number1:
if number3 > number2:
print("number3 is greater than number2")
else:
print("number3 is less than number2")
print("number3 is greater than number1")
else:
print("number1,number2 and number3 are equal")
except ValueError:
print("Sorry Invalid Input !!! Please enter Integer / Float Number")
Execution of Code (Dry Run)  
Python Statement  Calculations  Variable(s)  Output Screen 
try:  
number1 = float(input(“Enter First Number: “))  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300  
number2 = float(input(“Enter Second Number: “))  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550  
number3 = float(input(“Enter Third Number: “))  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120  
if number1 > number2:  Decision Making – Check Whether Condition is True or False number1 > number2 300 > 550 False (i.e., Statement(s) will be executed after ifStatement)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
if number1 > number3:  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
print(“number1 is greater than number3”)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
else:  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120  
print(“number1 is less than number3”)  This Statement will not be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
print(“number1 is greater than number2”)  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
elif number2 > number3:  Decision Making – Check Whether Condition is True or False number2 > number3 550 > 120 True (i.e., Statement(s) will be executed after ifStatement)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
if number2 > number1:  Decision Making – Check Whether Condition is True or False number2 > number3 550 > 300 True (i.e., Statement(s) will be executed after ifStatement)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 
print(“number2 is greater than number1”)  This Statement will be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 
else:  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1  
print(“number2 is less than number1”)  This Statement will not be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 
print(“number2 is greater than number3”)  This Statement will be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
elif number3 > number1:  Decision Making – Check Whether Condition is True or False number3 > number1 120 > 300 False (i.e., Statement(s) will be executed after ifStatement)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
if number3 > number2:  This Statement will not be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
print(“number3 is greater than number2”)  This Statement will not be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
else:  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3  
print(“number3 is less than number2”)  This Statement will be executed because Condition is False in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
print(“number3 is greater than number1”)  This Statement will not be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
else:  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3  
print(“number1,number2 and number3 are equal”)  This Statement will not be executed because Condition is True in above ifStatement (see Output Screen)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 
except ValueError:  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3  
print(“Sorry Invalid Input !!! Please enter Integer Number”)  number1 = 300 number2 = 550 number3 = 120  Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3  
Output of Code  
Enter First Integer Number: 300 Enter Second Integer Number: 550 Enter Third Integer Number: 120 number2 is greater than number1 number2 is greater than number3 