0% found this document useful (0 votes)
10 views42 pages

decision-control-structures-LOGmu5yT

This document covers decision control structures in programming, focusing on Boolean operators, conditional statements, and their application in problem-solving. It explains the differences between assignment and relational operators, outlines the structure of conditional statements, and introduces logical operators for compound Boolean expressions. The document also includes knowledge checks, quizzes, and discussions to reinforce learning outcomes.

Uploaded by

analindatoh.at
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views42 pages

decision-control-structures-LOGmu5yT

This document covers decision control structures in programming, focusing on Boolean operators, conditional statements, and their application in problem-solving. It explains the differences between assignment and relational operators, outlines the structure of conditional statements, and introduces logical operators for compound Boolean expressions. The document also includes knowledge checks, quizzes, and discussions to reinforce learning outcomes.

Uploaded by

analindatoh.at
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Lee Kien Phoon

Decision Control Structures

Learning Outcomes:
Upon successful completion of this lesson, you will be able to:

Explain the use of Boolean operators in evaluating a Boolean expression

Explain the structure of a conditional statement used in a control ow

Apply conditional statements for decision making in a program to solve


problem

Why Decision Control Structure

Relational Operators and Boolean Expressions

Difference between ‘=’ and ‘==’

Structure of a Conditional Statement

Logical Operators and Compound Boolean Expressions

Summary
TUTOR IAL

Knowledge Check

Quiz

Online Discussion
Lesson 1 of 9

Why Decision Control Structure


Lee Kien Phoon

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.

Bill Gates explains IF & IF/ELSE statements (about 1 min)


For example, if the condition "Cold outside" is True, we will wear a jacket before proceeding
to the next action.
The True condition can cause one or more statements to be executed.

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.


Parallelogram is used for input statement

age = int(input('Enter your age '))


Diamond is used for decision condition

if age < 18:


print('you are too young to drive now')
else:
print('you are of legal age to drive now')

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

Relational Operators and Boolean Expressions


Lee Kien Phoon

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

Difference between ‘=’ and ‘==’


Lee Kien Phoon

The = sign is an assignment operator. It is used to assign a value to a variable.


For example, x = y assigns the value of y to variable x. Variable x is changed after the
assignment statement is executed.

The == is a relational Operator. It is used to compare 2 values.


For example, x == y returns a True if x and y have the same value and False, otherwise.
Variable x is not changed after the comparison operation.

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

Structure of a Conditional Statement


Lee Kien Phoon

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.

Test Score Grade

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

Logical Operators and Compound Boolean


Expressions
Lee Kien Phoon

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.

Truth Table for "or" Operator



The "or" operator returns True if and only if both of its operand are false. It returns True
otherwise.
Truth Table for "not" Operator

The "not" operator returns its logical negation of its operand. That is, it returns True if the
expression is false and false if the expression if true.

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

x > z or y >= z False

not x True
x < z and y < z True

x != z and not y False


not x and not y False

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:

1. The minimum annual salary is 30000

2. The minimum years on the job is 2

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:

Relational boolean operators such as <, <=, >, >=, ==, !=

Logical boolean operators such as and, or, not

Simple and compound boolean expressions

The structure of a conditional statement used in a control ow.

How to apply conditional statements in a program to solve problem


Lesson 7 of 9

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:

(i > j) and (k < f)

Type your answer here

SUBMIT

(i != j) and (k != f)
Type your answer here

SUBMIT

(i == 1) or (j == 2) or (k != 4)

Type your answer here

SUBMIT

(i <= j) and (j >= k)

Type your answer here

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”.

num = int(input(“Enter Number:”))


num = int(input("Enter
Number: "))
if num == 0:
Try on your own before you
look at the answer.
print("Zero")
elif num <0:
print("Negative")
else:
Lesson 8 of 9

Quiz
Lee Kien Phoon

There are 6 questions in this quiz. You need to score 80% to pass the quiz.
Question

01/06

Which of the following is not a relational operator?

>=

==

and

!=

<
Question

02/06

What is the output of the following program?

temp = 29

if temp > 33:


msg = "Hot"
if temp > 28:
msg = "Warm"
if temp > 20:
msg = "Cool"

print(msg)

Hot

Warm

Cool

No output
Question

03/06

The Boolean expression, x and y, will return False if

x is True and y is True

x is True and y is False

x is False and y is True

x is False and y is False


Question

04/06

The Boolean expression, x or y, will return False if

x is True and y is True

x is True and y is False

x is False and y is True

x is False and y is False


Question

05/06

Assume that x = 1, y = 3 and z = 5. Which of the following boolean expressions


will return true?

not x == y

x == y or x < z

x != 6 and y > 10

x > 0 and x < 100

x > 0 or z < 0
Question

06/06

If A is True and B is False, what is the value of the following expression?


not (A or B)

Type your answer here


Lesson 9 of 9

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:

If your status is Single


and if the taxable income the tax is of the amount over
is
at most $32,000 10% $0
Over $32,000 $3,200 + 25% $32,000
If your status is Married
and if the taxable income the tax is of the amount over
is
at most $64,000 10% $0
Over $64,000 $6,400 + 25% $64,000

Design a program to compute the tax:

Draw the complete owchart of this program.

Implement the owchart by writing the python codes.

Discussion: Is there any alternative solution to this question?

You might also like