In The Current Lab Session, The Students Will Be Able To

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab # 2: Control Programming Flow (Conditional statements (if-else,

nested if statements)
OBJECTIVES
In the current Lab session, the students will be able to:
• Investigate the applications of conditional statements
• Implement If and If-else statement
• Explain the results of lab tasks through viva and technical report
• Demonstrate responsible attitude in lab execution.
CONDITIONAL STATEMENTS
The if statement is used to implement a decision (see Syntax of if statement below). When a “if”
condition is fulfilled, the body of if statement is executed. Otherwise, body of “else” is executed.

Here is an example using the if statement: In many countries, the number 13 is considered unlucky.
Rather than offending superstitious tenants, building owners sometimes skip the thirteenth floor; floor
12 is immediately followed by floor 14. Of course, floor 13 is not usually left empty or, as some
conspiracy theorists believe, filled with secret offices and research labs. It is simply called floor 14. The
computer that controls the building elevators needs to compensate for this foible and adjust all floor
numbers above 13.

Let’s simulate this process in Python. We will ask the user to type in the desired floor number and then
compute the actual floor. When the input is above 13, then we need to decrement the input to obtain
the actual floor.

EXAMPLE 1
##
# This program simulates an elevator panel that skips the 13th floor.
#

# Obtain the floor number from the user as an integer.


floor = int(input("Floor: "))

# Adjust floor if necessary.


if floor > 13 :
actualFloor = floor - 1
else :
actualFloor = floor

# Print the result.


print("The elevator will travel to the actual floor", actualFloor)

Every if statement contains a condition. In many cases, the condition involves comparing two values. For
example, in the previous examples we tested floor > 13. The comparison > is called a relational operator.
Python has six relational operators as shown in Table 1.

Example 2
a=5
b=8

if a > b :
print( "a is greater than b" )

if b > a :
print( "b is greater than a" )

if a == b :
print( "a is equal to b" )

Example 3: Height of siblings


name1 = "Dale"
print ("Enter the height of %s in cm ==> " %name1)
height1 = int(input())

name2 = "Erin"
print ("Enter the height of %s in cm ==> " %name2)
height2 = int(input())

if height1 < height2:


print ("%s is taller" %name2)
max_height = height2

if height1 >= height2:


print ("%s is taller" %name1)
max_height = height1

print ("The max height is %d" %max_height)

Using If – Else statement:

Example 4

a=5
b=8
if a > b :
print ( "a is greater b" )
else :
print ( "b is greater than a or a and b are equal " )

Example 5: Height of siblings

name1 = "Dale"
print ("Enter the height of %s in cm ==> " %name1)
height1 = int(input())

name2 = "Erin"
print ("Enter the height of %s in cm ==> " %name2)
height2 = int(input())
if height1 < height2:
print ("%s is taller" %name2)
max_height = height2

else:
print ("%s is taller" %name1)
max_height = height1

print ("The max height is %d" %max_height)

Using Elif statement:

Example 6

a=5
b=8

if a > b :
print ( "a is greater than b" )
elif b > a :
print ( "b is greater than a" )
elif a == b :
pr in t ( "a is equal to b" )

Note! Python uses "elif" not "elseif" like many other programming languages do.

Example 7: Calculate Grade of Student

print("Enter marks ");

mark = int(input());

if(mark>=91 and mark<=100):

print("Your Grade is A+")

elif(mark>=81 and mark<=90):

print("Your Grade is A")

elif(mark>=71 and mark<=80):

print("Your Grade is B+")

elif(mark>=61 and mark<=70):

print("Your Grade is B")

elif(mark>=51 and mark<=60):

print("Your Grade is C+")

elif(mark>=41 and mark<=50):


print("Your Grade is C")

elif(mark>=0 and mark<=40):

print("Your Grade is F")

else:

print("Strange Grade..!!")

You might also like