decision-control-structures-LOGmu5yT
decision-control-structures-LOGmu5yT
Learning Outcomes:
Upon successful completion of this lesson, you will be able to:
Summary
TUTOR IAL
Knowledge Check
Quiz
Online Discussion
Lesson 1 of 9
Programs are executed in sequence, known as sequential control structure. Often, we need to
tell the computer to do something only when some conditions occur. This will alter the
sequential execution of the program.
Let's look at an example. A program asks the user to enter age and if the age is less than 18, it
will display a message "You are too young to drive now". Otherwise, it will display "You are of
legal age to drive now".
The owchart for the example will look like this. Click to show the corresponding Python
code.
Knowledge Check
A program asks the user to enter age and if the age is less than 0, it will display a message
"Invalid Input!". Draw the owchart.
Answer
Lesson 2 of 9
Before we can test the conditions in a Python program, we need to understand relational
operators and boolean expressions.
Relational Operators are used for comparing two values. The result of the comparison (or
boolean expression) is a boolean value, either True or False. The relational operators and their
corresponding boolean expression is given below:
For example, if we ask the question "Is x greater than y?", it will be True if yes and False if
otherwise.
Knowledge Check
Assume that x = 3, y = 2 and z = 3, state if the following boolean expression is true or false:
x>y True
y >= z False
x != z False
z<x False
z == x True
Lesson 3 of 9
Example
x = 6 is assigning the value of 6 to x.
y = 5 is assigning the value of 5 to y.
z = 5 is assigning the value of 5 to z.
(x==y) is False because we assigned di erent values to x and y.
(y==z) is True because we assign equal values to y and z.
Lesson 4 of 9
In order to make a decision for the next course of action, a program must be able to test one
or more conditions. There are three di erent structures for a conditional statement.
One-way Decision
–
A one-way decision is the the simplest form of the conditional statement as it consists of a
condition and just a single sequence of statements. if the condition is true, the sequence of
statements is executed. Otherwise, control ows to the next statement after the entire
conditional statement.
Example
If a salesman exceeds the sales quota, a bonus and a commission rate is given.
Two-way Decision
–
In many cases, a program is directed to make a choice between two alternative courses of
actions.
Example
If a salesman exceeds the sales quota, a bonus and a commission rate is given. Otherwise, only
a message is displayed to inform that the sales quota is not met.
Multi-way Decision)
–
Sometimes, a program is faced with testing several conditions that involves more than two
alternative courses of action.
Example
Assume that the salesman are given di erent bonus and commission rate, depending on the
sales amount.
Indentation
Proper indentation in Python is very important. Wrong indentation can result in syntax
and/or logic error.
How to Use If Else Statements in Python (about 13 mins)
Knowledge Check
The following table shows the grading for a test. Draw a owchart to ask user for a test score,
determine the grade and display it on the screen. Write the Python code.
80 and above A
70 - 79 B
60 - 69 C
60 - 69 D
Below 50 F
Flowchart
–
Depending on your logic, your owchart may be di erent from the given answer. Check with
your tutor if you are in doubt.
Python Code
–
The Python code should follow the owchart logic. Bear in mind that we design the program
using owchart or pseudocode, then convert it to a programming language of our choice.
Lesson 5 of 9
In many situations, we need to make decisions based on two or more conditions. Logical
operators allow us to combine multiple boolean expressions to create a compound boolean
expression. There are three logical operators in Python as shown in the rst table. The
second table shows the compound boolean expressions.
Truth Table
The "and" operator and the "or" operator expect two operands. The "not" operator expects a
single operand. The behavior of each logical operator can be speci ed using a truth table for
that operator.
Truth Table for "and" Operator
–
The "and" operator returns True if and only if both of its operand are true. It returns False
otherwise.
Knowledge Check
Assume that x = 0, y = 2 and z = 3, state if the following compound boolean expression is true
or false:
x > y or x < z True
not x True
x < z and y < z True
Knowledge Check
Design a program to check if a customer is quali ed for a loan. The required conditions to be
quali ed for the loan are:
Flowchart
–
There are more than one solution in this problem. Below is one of them.
Python Code
–
Lesson 6 of 9
Summary
Lee Kien Phoon
Key Takeaways
We have covered the following:
Knowledge Check
Lee Kien Phoon
Question 1
Given these following declarations and initializations:
i=1
j=2
k=4
f = 1.0
Indicate True (T) or False (F) for each of the following expression:
SUBMIT
(i != j) and (k != f)
Type your answer here
SUBMIT
(i == 1) or (j == 2) or (k != 4)
SUBMIT
SUBMIT
Question 2
Given x = 1, and y = 2, what is the output of the following code?
if x == y:
No output.
print(“Statement 1. ”)
if x == y:
print (“Statement 1. ”) Statement 2.
print (“Statement 2. ”)
if x == y:
i (“S 1 ”) N t t
print(“Statement 1. ”) No output.
print(“Statement 2. ”)
if x == y:
print(“Statement 1. ”)
Statement 2.
else:
print(“Statement 2. ”)
if x != y:
print(“Statement 1. ”)
else: Statement 1.
print(“Statement 2. ”)
print(“Statement 3. ”)
if x == y:
print(“Statement 1. ”)
if x < y:
Statement 3.
print(“Statement 2. ”)
else:
print(“Statement 3. ”)
if x == y:
print(“Statement 1. ”)
else: Statement 2.
if x < y:
print(“Statement 2. ”)
Question 3
The following codes read in a number from the keyboard. Write Python codes to print the
statement “Zero” if the number entered is 0. Otherwise, print “Positive” or “Negative”.
Quiz
Lee Kien Phoon
There are 6 questions in this quiz. You need to score 80% to pass the quiz.
Question
01/06
>=
==
and
!=
<
Question
02/06
temp = 29
print(msg)
Hot
Warm
Cool
No output
Question
03/06
04/06
05/06
not x == y
x == y or x < z
x != 6 and y > 10
x > 0 or z < 0
Question
06/06
Online Discussion
Lee Kien Phoon
In certain countries, di erent tax rates are used depending on the taxpayer’s marital status
and taxable income. An example of the tax table is as shown: