Chapter 5

advertisement
Programming Logic and Design, Fourth Edition, Introductory
5-1
Chapter 5
Making Decisions
Lecture Notes
Overview
Chapter 5 provides an introduction to decision making with loops and case structures. It also introduces relational
comparison operators, and the use of AND and OR logic. Students will learn to make selections within ranges,
and to understand the precedence of AND and OR structures. Finally, students are introduced to the use of
decision tables as an aid for designing programs.
Evaluating Boolean Expressions to Make Comparisons
1. Review the concept of selection structures.
2. Contrast the dual-alternative selection with the single-alternative selection.
3. Describe the logic shown in Figure 5-3. Discuss the importance of the order of the
statements in the pseudocode for the overtime pay calculation.
4. Describe the logic shown in Figure 5-4, and contrast it with the previous figure.
5. Introduce the concept of the Boolean expression.
Using the Relational Comparison Operators
1. Discuss the six possible ways to compare two values.
2. Introduce relational comparison operators, and point out that these operators are used to
build Boolean expressions.
3. Discuss why the <= and the >= operators make code more understandable.
4. Discuss the need to adjust the Boolean expression and the logic based on the
comparison being used.
5. Discuss the reasoning behind asking the question more likely to have a positive
outcome.
Programming Logic and Design, Fourth Edition, Introductory
5-2
Quick Quiz 1
1. Many restaurants offer Senior meals for persons 55 and older. How might you write a
selection question to select all persons eligible for Senior meals?
Answer: If age >= 55
2. When should a single-alternative decision structure be used instead of a dual-alternative
structure?
Answer: When no action is to be taken only when the decision expression is false.
3. What values can result from evaluating Boolean expressions?
Answer: True or False
Understanding AND Logic
1. Introduce the concept of an AND decision, in which both tests must evaluate to True.
Combining Decisions in an AND Selection
1. Introduce the concept of using a logical AND operator to combine two or more
questions in a single statement.
Understanding OR Logic
1. Introduce the concept of the OR decision, in which only one of the conditions must be
true to produce a true result.
2. Point out that if the first condition is true, there is no need to test the second condition.
3. Walk through the sample application shown in Figure 5-20. Point out that the required
output this time is for employees who have either medical or dental insurance.
Avoiding Common Errors in an OR Selection
1. Describe the error of creating an OR selection.
2. Discuss the problems that casual use of the word AND can cause when the logic
requires an OR decision.
3. Describe the error of incorrectly interpreting English to use an OR when AND logic is
required.
Writing OR Decisions for Efficiency
1. Discuss the importance of analyzing the efficiency of how the conditions are asked.
Programming Logic and Design, Fourth Edition, Introductory
5-3
2. Point out that the question more likely to be true will eliminate the need to ask the
second question.
Combining Decisions in an OR Selection
1. Introduce the concept of using a logical OR operator to combine two or more questions
in a single statement.
2. Point out that only one of the Boolean expressions must be true to return a value of true
for the entire statement.
3. Describe how the computer processes a logical OR operator, using a chained if
statement. Contrast this to the logical AND operator.
Quick Quiz 2
1. Which logical operator requires that only one condition be true to produce a true result?
Answer: OR
2. Which logical operator requires that both conditions be true to produce a true result?
Answer: AND
3. In an OR decision, you should first ask the question that is (more or less?) ____ likely
to be true.
Answer: more
4. In an AND decision, you should first ask the question that is (more or less?) ____ likely
to be true.
Answer: less
Using Selections Within Ranges
1. Introduce the concept of a range check.
2. Discuss the alternatives of using either the highest or the lowest value in each range of
values.
3. Point out the need to adjust the question logic for using highest or lowest values by
comparing Figures 5-31 and 5-32.
Common Errors Using Range Checks
1. Discuss the error of testing values (using Figure 5-33) that lead to dead or unreachable
paths.
Programming Logic and Design, Fourth Edition, Introductory
5-4
2. Describe the error of asking unnecessary questions, and the two possible conditions that
might cause this.
Understanding Precedence When Combining AND and OR Selections
1. Point out that you can combine many AND and OR operators in an expression.
2. Describe the use of multiple AND operators when multiple conditions must all be true.
3. Contrast the use of multiple OR operators when only one of multiple conditions must be
true.
4. Discuss the precedence of the AND operator when combined with the OR operator in a
single statement, and the use of parentheses to force
5. Point out that using too many AND and OR operators in a single statement will make
the code less readable.
Understanding the Case Structure
1. Introduce the concept of the case structure as a means to handle multiple alternatives
based on the value of a single variable.
2. Point out that the case structure replaces a series of chained if-else statements.
Contrast Figures 5-36 and 5-37 for simplicity and readability, and point out the
advantages of using the case structure.
Quick Quiz 3
1. For a decision with two alternatives, you should use a(n) ________ structure, while for
a decision with four alternatives, you should use a(n) _______ structure.
Answer: if-else, case
2. When AND and OR operators are combined in the same statement, which has
precedence?
Answer: AND
3. A(n) ____________ structure can be replaced by a chained if decision.
Answer: OR
Programming Logic and Design, Fourth Edition, Introductory
5-5
Key Terms




















AND decision: two conditions must both be true for an action to take place.
Boolean expression: one that represents only one of two states, usually expressed as
true or false.
Case structure: provides a convenient alternative to using a series of decisions when
you must make choices based on the value stored in a single variable.
Dead or unreachable path: a logical path that can never be traveled.
Decision table: a problem-analysis tool that consists of four parts: conditions, possible
combinations of Boolean values for the conditions, possible actions based on the
conditions, and the specific action that corresponds to each Boolean value of each
condition.
Default value: one that is assigned after all test conditions are found to be false.
Dual-alternative, or binary, selection structure: offers two actions, each associated
with one of two possible outcomes.
else clause of a decision: holds the action or actions that execute when the Boolean
expression in a decision is false.
if clause of a decision: holds the action or actions that execute when a Boolean
expression in a decision is true.
Logical AND operator: a symbol that you use to combine decisions so that two (or
more) conditions must be true for an action to occur.
Logical operators: (as the term is most often used) compares single bits. However,
some programmers use the term synonymously with “relational comparison operator.”
Logical OR operator: a symbol that you use to combine decisions when any one
condition can be true for an action to occur.
OR decision: contains two (or more) decisions; if at least one condition is met, the
resulting action takes place.
Precedence: When an operator has precedence, it is evaluated before others.
Range check: when you compare a variable to a series of values between limits.
Range of values: encompasses every value between a high and low limit.
Relational comparison operators: the symbols that express Boolean comparisons.
Examples include =, >, <, >=, <=, and <>.
Short-circuiting: the compiler technique of not evaluating an expression when the
outcome makes no difference.
Single-alternative, or unary, selection structure: an action is required for only one
outcome of the question.
Trivial Boolean expression: one that always evaluates to the same result.
Download