Lab 04 - Conditional Statement
Lab 04 - Conditional Statement
Lab # 4
Use of Conditional Statements, and Switch case
OBJECTIVE:
To get familiar with the concept of conditional statement for simple decision
making and nested condition for complex cases in addition with match case.
THEORY:
Decision making statements in programming languages decides the direction of
flow of program execution.
The if…elif…else statement is used in Python for decision making.
The if Statement
Like most languages, python uses the keyword if to implement the decision
control instruction. It is used to decide whether a certain statement or block of
statements will be executed or not i.e if a certain condition is true then a block of
statement is executed otherwise not. The general form of if statement looks like
this:
Syntax:
if condition:
# Statements to execute if
# condition is true
As a general rule, we express a condition using python’s ‘relational’ operators.
The relational operators allow us to compare two values to see whether they are
equal to each other, unequal, or whether one is greater than the other. Here is how
they look and how they are evaluated in python.
Example:
# python program to illustrate If statement
i = 10
if (i > 15):
print (i, "is greater than 15")
print ("I am not greater")
Output:
>>> %Run task1.py
I am not greater
>>>
Example:
# python program to illustrate If else statement
i = 20;
if (i < 15):
print (i,“is smaller than 15")
print ("i'm in if Block")
else:
print (i,"is greater than 15")
print ("i'm in else Block")
print ("i'm not in if and not in else Block")
Output:
>>> %Run task2.py
20 is greater than 15
i'm in else Block
i'm not in if and not in else Block
>>>
.
.
else:
statement
Example:
# Python program to illustrate if-elif-else
i = 30
if (i == 10):
print ("i is 10")
elif (i == 20):
print ("i is 20")
elif (i == 30):
print ("i is 30")
else:
print ("i is not present")
Output:
>>> %Run task3.py
i is 30
>>>
Syntax:
if condition1:
statement(s)
elif condition2:
statement(s)
else:
statement(s)
Example:
a=20 b=10 c=30
if a >= b and a >= c:
print "a is big"
elif b >= a and b >= c:
Output:
c is big
Example: Write a program for checking the height eligibility of male and
female in a military qualification round after taking values as input:
Code:
g=(input("Enter Gender "))
h = float(input("Enter Height "))
var_m = 5.4
var_f = 5.0
if g == 'M' or g=="m":
if h >= var_m:
print("Allowed Male Height")
else:
print("Not Allowed Male Height")
elif g == 'F' or g == 'f':
if h >= var_f:
print("Allowed FeMale Height")
else:
print("Not Allowed FeMale Height")
else:
print("Undefined Gender")
Output-1: Output-2:
Enter Gender M Enter Gender f
Enter Height 5.2 Enter Height 5.2
Not Allowed Male Height Allowed FeMale Height
MATCH CASE
For the numerous potential results of a given variable, we can create numerous
case statements. A pattern must be matched in each case statement.
Let’s look at how to implement it:
Syntax:
match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>
Example:
user_input = int(input("enter your lucky number between 1-5\n"))
match user_input:
case 1:
print( "you will have a new house")
case 2:
print( "you will receive good news ")
case 3:
print( "you will get a car")
case 4:
print( "you might face your fear this week")
case 5:
print( "you will get a pet")
EXERCISE
A. Point out the errors, if any, and paste the output also in the following
Python programs.
1. Code
a = 500,b,c;
if ( a >= 400 ):
b = 300
c = 200
print( "Value is:", b, c )
2. Code
&number = eval(input("Enter an integer: "))
print(type(number))
if number % 5 == 0
print("HiFive")
else
print("No answer")
3. Code
if score >= 60.0
grade = 'D'
elif score >= 70.0
grade = 'C'
elif score >= 80.0
grade = 'B'
elif score >= 90.0
grade = 'A'
else:
grade = 'F'
Output:
2. Code
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output
3. Code
age = 15
if age < 4:
price = 0
elif age < 18:
price = 1500
else:
price = 2000
print("Your admission cost is Rs" + str(price) + ".")
Output
• If the person is at least 2 years old but less than 4, print a message that the person is a
toddler.
• If the person is at least 4 years old but less than 13, print a message that the person is
a kid.
• If the person is at least 13 years old but less than 20, print a message that the person is
a teenager.
• If the person is at least 20 years old but less than 65, print a message that the person is
an adult.
• If the person is age 65 or older, print a message that the person is an elder.
4. Write a source code that will check the age of a person if above 18 then check
CNIC is made finally allow that person to apply for driving license.
D. Tasks to Report:
• What is the advantage of switch case over if statement?
When we use nested if statement?