Professional Documents
Culture Documents
In The Current Lab Session, The Students Will Be Able To
In The Current Lab Session, The Students Will Be Able To
In The Current Lab Session, The Students Will Be Able To
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.
#
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" )
name2 = "Erin"
print ("Enter the height of %s in cm ==> " %name2)
height2 = int(input())
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 " )
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
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.
mark = int(input());
else:
print("Strange Grade..!!")