Unit 4 - Making Decision
Unit 4 - Making Decision
Unit 4 - Making Decision
Learning Outcomes
• Boolean expressions and the selection structure
• The relational comparison operators
• AND logic
• OR logic
• Making selections within ranges
• Precedence when combining AND and OR
operators
Add two numbers
• Step 1: Start
• Step 2: Declare variables
num1, num2 and sum.
• Step 3: Read values for
num1, num2.
• Step 4: Add num1 and
num2 and assign the result
to a variable sum.
• Step 5: Display sum
• Step 6: Stop
Boolean Expressions and the
Selection Structure
Every decision you make in a computer program
involves evaluating a Boolean expression—an
expression whose value can be only true or false.
pseudocode and flowchart to calculate the
Interest of a Bank Deposit.
Step 1: Read amount,
Step 2: Read years,
Step 3: Read rate,
Step 4: Calculate the
interest with the formula
"Interest=Amount*Years*R
ate/100
Step 5: Print interest
Roots of a quadratic equation
Whether Number N is Even or Odd
Step 1: Read number N,
Step 2: Set remainder as N
modulo 2,
Step 3: If the remainder is
equal to 0 then number N
is even, else number N is
odd,
Step 4: Print output.
Sum of the digits of a given number.
Step 1: Input N
Step 2: Sum = 0
Step 3: While (N != 0)
Rem = N % 10;
Sum = Sum + Rem;
N = N / 10;
Step 4: Print Sum
Largest number between two numbers
Largest number between three numbers
Two clauses of decision
• The if-then clause is the part of the decision
that holds the action or actions that execute
when the tested condition in the decision is
true.
• The else clause of the decision is the part that
executes only when the tested condition in the
decision is false.
True/False
1. The if-then clause is the part of a decision that
executes when a tested condition in a
decision is true.
2. The else clause is the part of a decision that
executes when a tested condition in a decision
is true.
3. A Boolean expression is one whose value is
true or false.
Relational Comparison Operators
Examples
Example
Example
True/False
1. Usually, you can compare only values that are
of the same data type.
2. A Boolean expression is defined as one that
decides whether two values are equal.
3. In any Boolean expression, the two values
compared can be either variables or
constants.
AND Logic
• Often, you need to evaluate more than one
expression to determine whether an action
should take place. When you ask multiple
questions before an outcome is determined, you
create a compound condition.
• An AND decision—a decision in which two
conditions must be true for an action to take
place.
AND Operator
• Most programming languages allow you to ask two
or more questions in a single comparison by using a
conditional AND operator, or more simply, an AND
operator that joins decisions in a single statement.
• One tool that can help you understand the AND
operator is a truth table. Truth tables are diagrams
used in mathematics and logic to help describe the
truth of an entire expression based on the truth of
its parts.
Truth table of AND
Example
Example
OR Logic
• Sometimes you want to take action when one
or the other of two conditions is true. This is
called an OR decision because either one
condition or some other condition must be met
in order for an event to take place.
Example
Example
OR Operator
If you need to take action when either one or the
other of two conditions is met, you can use two
separate, nested selection structures. However,
most programming languages allow you to ask two
or more questions in a single comparison by using
an OR operator.
Truth table for the OR operator
Example
Example
Making Selections within Ranges
You often need to take action when a variable falls
within a range of values.
Precedence When Combining AND
and OR Operators
• Most programming languages allow you to combine
as many AND and OR operators in an expression as
you need.
• The logic becomes more complicated when you
combine AND and OR operators within the same
statement. When you do, the AND operators take
precedence, meaning the Boolean values of their
expressions are evaluated first.
Example
To avoid confusion when mixing
AND and OR operators
• You can use parentheses to override the default
order of operations.
• You can use parentheses for clarity even though
they do not change what the order of
operations would be without them.
• You can use nesting if statements instead of
using AND and OR operators.
Discussion
1. Computer programs can be used to make decisions about
your insurability as well as the rates you will be charged for
health and life insurance policies. For example, certain
preexisting conditions may raise your insurance premiums
considerably. Is it ethical for insurance companies to access
your health records and then make insurance decisions
about you? Explain your answer.
2. Job applications are sometimes screened by software that
makes decisions about a candidate’s suitability based on
keywords in the applications. Is such screening fair to
applicants? Explain your answer.
References
Required reading:
• Programming Logic and Design, Comprehensive,
Cengage Learning, 2018 by Joyce Farrell
Reference Materials:
• Starting Out with Programming Logic and
Design, Pearson Education, 2015 by Tony Gaddis
THANK YOU
For your time!