Lab 3
Lab 3
Objectives
• To understand the programming pattern simple/two-
way/multiway decision and its implementation using a
Python if/if-else/if-elif-else statement.
• To understand the concept of Boolean expressions and
the bool data type
• To be able to read, write, and implement algorithms that
employ decision structures, including those that employ
sequences of decisions and nested control structures
• To understand the concepts of definite and indefinite
loops as they are realized in the Python for and while
statements.
Control structures
• Sequential control vs Selection control vs Iterative control
• A control statement is a statement that determines the
control flow of a set of statements
• A control structure is a set of statements and the control
statements controlling their execution
• Three fundamental forms of control in programming are
• Sequential
• Selection
• Iteration
Simple decisions
• For example,
if n < 1:
print("Your input number is too low.")
if n > 10000:
print("Your input number is too large.")
• A relational statement gives either True or False
(Python's keywords)
• Try
• print(int(True))
• print(int(False))
To do or not to do
• The Python if statement is used to implement the
decision.
if <condition>:
<body>
• The body is a sequence of one or more statements
indented under the if heading
• The body is executed if condition is evaluated to True
• The body is skipped if condition is evaluated to False
Boolean Expressions (Conditions)
• The Boolean data type contains two Boolean values,
denoted as True and False in Python
• A Boolean expression is an expression that evaluates to
a Boolean value
• Need a relational operator to evaluate a boolean
expression
• The relational operators on the next slide can be applied
to any set of values that has an ordering
• Number comparison
• Lexicographical ordering for string comparison
Relational Operators
Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
EXERCISE 3.3
Try
•True and False; True or False
•not (True) and False; not (True and False)
•(10 < 0) and (10 > 2); (10 < 0) or (10 > 2)
•not (10 < 0) or (10 > 2); not (10 < 0 or 10 > 2)
Operator Precedence
Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
EXERCISE 3.4
Try to figure out the answers before running them:
•True or False and True and True
•True or not False and True and not True
•((True or not False) and True) and not True
Two-Way Decisions
• In Python, a two-way decision can be implemented by
attaching an else clause onto an if clause.
• This is called an if-else statement:
if <condition>:
<statements>
else:
<statements>
• E.g.,
if 1 <= n and n <= 10000:
print("Your input number is " + str(n) + ".")
else:
print("Your input must be between 1 and 10000.")
Multi-Way Decisions
• Use nested if-else statement to implement multi-way
decisions.
• if <condition1>:
<case1 statements>
• else:
if <condition2>:
<case2 statements>
else:
if <condition3>:
<case3 statements>
else:
<default statements>
Multi-Way Decisions using elif
• In Python, else-if can be combined into elif:
if <condition1>:
<case1 statements>
elif <condition2>:
<case2 statements>
elif <condition3>:
<case3 statements>
else:
<default statements>
i = 0
while i <= 9:
print(i)
i = i + 1
for i in range(10):
print i
EXERCISE 3.7
Use a while loop to print out range(2, 10, 2).
Note: The 3rd argument of range() here means
each time the number is incremented by 2.
EXERCISE 3.8
Try
i = 0
while i <= 9:
print(i)
Interactive Loops
# average2.py
# A program to average a set of numbers
# Illustrates interactive loop with two accumulators
moredata = "yes"
sum = 0.0
count = 0
while moredata[0] == 'y':
x = int(input("Enter a number >> "))
sum = sum + x
count = count + 1
moredata = input("Do you have more numbers (yes or no)? ")