Programming Logic and
Design
Fourth Edition, Introductory
Chapter 5
Making Decisions
Objectives
•
•
•
•
•
Evaluate Boolean expressions to make comparisons
Use the relational comparison operators
Understand AND logic
Understand OR logic
Use selections within ranges
Programming Logic and Design, Introductory, Fourth Edition
2
Objectives (continued)
• Understand precedence when combining AND and
OR selections
• Understand the case structure
• Use decision tables
Programming Logic and Design, Introductory, Fourth Edition
3
Evaluating Boolean Expressions to
Make Comparisons
• Dual-alternative (or binary) selection structure:
– Provides an action for each of two possible outcomes
Programming Logic and Design, Introductory, Fourth Edition
4
Evaluating Boolean Expressions to
Make Comparisons (continued)
• Single-alternative (or unary) selection structure
– Action is provided for only one outcome
Programming Logic and Design, Introductory, Fourth Edition
5
Evaluating Boolean Expressions to
Make Comparisons (continued)
• Dual-alternative (or binary) selection structure:
– Also called an if-then-else structure
Programming Logic and Design, Introductory, Fourth Edition
6
Evaluating Boolean Expressions to
Make Comparisons (continued)
• Single-alternative (or unary) selection structure
– Also called an if-then structure
Programming Logic and Design, Introductory, Fourth Edition
7
Evaluating Boolean Expressions to
Make Comparisons (continued)
• Boolean expression
– Represents only one of two states
– Expression evaluates to either true or false
• Expressions with relational operators produce
Boolean results:
hours worked > 40
Programming Logic and Design, Introductory, Fourth Edition
8
Using the Relational Comparison
Operators
• Six possible ways to compare two values:
–
–
–
–
–
–
Both are equal
The first is greater than the second
The first is less than the second
The first is greater than or equal to the second
The first is less than or equal to the second
The two values are not equal
Programming Logic and Design, Introductory, Fourth Edition
9
Using the Relational Comparison
Operators (continued)
•
Relational comparison operators:
– To express Boolean tests when comparing values
•
Different languages use different symbols
– Equals: =
– Less than: <
– Greater than: <
– Less than or equal: <=
– Greater than or equal: >=
Programming Logic and Design, Introductory, Fourth Edition
10
Using the Relational Comparison
Operators (continued)
• Any logical situation can be expressed with only three
types of comparisons: =, >, and <
• >= and <= are not necessary, but make code more
readable
• Adjust the logic based on the comparison type
Programming Logic and Design, Introductory, Fourth Edition
11
Using the Relational Comparison
Operators (continued)
• Rule of thumb: ask the question most likely to have a
positive outcome
• Avoid “not equal” when it results in a double negative
Programming Logic and Design, Introductory, Fourth Edition
12
Using the Relational Comparison
Operators (continued)
• Rephrase in the positive
• Some comparisons are clearer when negative is used
Programming Logic and Design, Introductory, Fourth Edition
13
Using the Relational Comparison
Operators (continued)
Programming Logic and Design, Introductory, Fourth Edition
14
Understanding AND Logic
• AND decision
– Requires that both of two tests evaluate to True
– Requires a nested decision (nested if)
Programming Logic and Design, Introductory, Fourth Edition
15
Understanding AND Logic (continued)
• Developing the application
– The input data
Programming Logic and Design, Introductory, Fourth Edition
16
Understanding AND Logic (continued)
• Developing the application
– The intended output:
Programming Logic and Design, Introductory, Fourth Edition
17
Understanding AND Logic (continued)
• Developing the application
– The mainline logic:
Programming Logic and Design, Introductory, Fourth Edition
18
Understanding AND Logic (continued)
• housekeeping() module:
Programming Logic and Design, Introductory, Fourth Edition
19
Understanding AND Logic (continued)
• createReport() module:
Programming Logic and Design, Introductory, Fourth Edition
20
Understanding AND Logic (continued)
Programming Logic and Design, Introductory, Fourth Edition
21
Writing Nested AND Decisions for
Efficiency
• For nested decisions, decide which to make first
• An appropriate choice may improve performance
• Consider expected outcomes to determine the most
efficient way to nest the decisions
Programming Logic and Design, Introductory, Fourth Edition
22
Writing Nested AND Decisions for
Efficiency (continued)
• With 1000 employees, of which 90% (900) are in the
medical plan, and 50% (500) are in the dental plan:
– First question is asked 1000 times
– Second question is asked 900 times
Programming Logic and Design, Introductory, Fourth Edition
23
Writing Nested AND Decisions for
Efficiency (continued)
• With 1000 employees, of which 90% (900) are in the
medical plan, and 50% (500) are in the dental plan:
– First question is asked 1000 times
– Second question is asked 500 times
Programming Logic and Design, Introductory, Fourth Edition
24
Writing Nested AND Decisions for
Efficiency (continued)
• This analysis may not be possible:
– You may not know decision outcome likelihoods
– The decisions may not be mutually exclusive
• Rule of Thumb: First ask the question that is less
likely to be true
– Reduces the number of times the second question will
need to be asked
Programming Logic and Design, Introductory, Fourth Edition
25
Combining Decisions in an AND
Selection
• Logical AND operator:
– Allows you to ask two or more questions (Boolean
expressions) in a single comparison
– Each Boolean expression in an AND selection must be
true to produce a result of true
– Question placed first will be asked first, so consider
efficiency
Programming Logic and Design, Introductory, Fourth Edition
26
Combining Decisions in an AND
Selection (continued)
Programming Logic and Design, Introductory, Fourth Edition
27
Combining Decisions in an AND
Selection (continued)
Programming Logic and Design, Introductory, Fourth Edition
28
Avoiding Common Errors in an AND
Selection
• Failure to nest 2nd decision entirely within 1st decision
Programming Logic and Design, Introductory, Fourth Edition
29
Avoiding Common Errors in an AND
Selection (continued)
• Incorrect questions to determine inclusion in a range
• Correct way:
Programming Logic and Design, Introductory, Fourth Edition
30
Avoiding Common Errors in an AND
Selection (continued)
• Failure to use two complete Boolean expressions:
• Correct way:
Programming Logic and Design, Introductory, Fourth Edition
31
Understanding OR Logic
• OR decision
– At least one of two conditions must be true to produced
a result of True
– If first condition is true, no need to test the second
condition
Programming Logic and Design, Introductory, Fourth Edition
32
Understanding OR Logic (continued)
• createReport() module:
Programming Logic and Design, Introductory, Fourth Edition
33
Avoiding Common Errors in an OR
Selection
• Unstructured OR selection:
Programming Logic and Design, Introductory, Fourth Edition
34
Avoiding Common Errors in an OR
Selection (continued)
• Incorrect interpretation of English:
– Casual use of AND when logic requires OR
Programming Logic and Design, Introductory, Fourth Edition
35
Avoiding Common Errors in an OR
Selection (continued)
• Correct logic:
Programming Logic and Design, Introductory, Fourth Edition
36
Avoiding Common Errors in an OR
Selection (continued)
• Incorrect interpretation of English
– Use of OR when AND logic is required
Programming Logic and Design, Introductory, Fourth Edition
37
Avoiding Common Errors in an OR
Selection (continued)
• Correct logic:
Programming Logic and Design, Introductory, Fourth Edition
38
Writing OR Decisions for Efficiency
• How many decisions?
Programming Logic and Design, Introductory, Fourth Edition
39
Writing OR Decisions for Efficiency
(continued)
Programming Logic and Design, Introductory, Fourth Edition
40
Writing OR Decisions for Efficiency
(continued)
• Both produce the same output, but vary widely in
number of questions asked
• If first question is true, no need to ask second
• Rule of thumb:
– First ask the question that is more likely to be true
Programming Logic and Design, Introductory, Fourth Edition
41
Combining Decisions in an OR
Selection
• Logical OR operator:
– Allows you to ask two or more questions (Boolean
expressions) in a single comparison
– Only one Boolean expression in an OR selection
must be true to produce a result of true
– Question placed first will be asked first, so consider
efficiency
Programming Logic and Design, Introductory, Fourth Edition
42
Combining Decisions in an OR
Selection (continued)
• Using an OR operator:
Programming Logic and Design, Introductory, Fourth Edition
43
Combining Decisions in an OR
Selection (continued)
• What the computer actually does:
Programming Logic and Design, Introductory, Fourth Edition
44
Using Selections Within Ranges
• Range check: compare a variable to a series of
values between limits
• Use the lowest or highest value in each range
• Adjust the question logic when using highest
versus lowest values
• Should end points of the range be included?
– Yes: use >= or <=
– No: use < or >
Programming Logic and Design, Introductory, Fourth Edition
45
Using Selections Within Ranges
(continued)
• Using high-end values in the range check:
Programming Logic and Design, Introductory, Fourth Edition
46
Using Selections Within Ranges
(continued)
• Using low-end values in the range check:
Programming Logic and Design, Introductory, Fourth Edition
47
Common Errors Using Range Checks
• Avoid dead or unreachable paths
– Don’t check for values that can never occur
– Requires some prior knowledge of the data
Programming Logic and Design, Introductory, Fourth Edition
48
Common Errors Using Range Checks
(continued)
Programming Logic and Design, Introductory, Fourth Edition
49
Common Errors Using Range Checks
(continued)
• Avoid asking an unneeded question
– If there is only one possible outcome
– If previous logic has already determined the answer
Programming Logic and Design, Introductory, Fourth Edition
50
Common Errors Using Range Checks
(continued)
Programming Logic and Design, Introductory, Fourth Edition
51
Understanding Precedence When
Combining AND and OR Selections
• Can combine multiple AND and OR operators in an
expression
• When multiple conditions must all be true, use
multiple ANDs
Programming Logic and Design, Introductory, Fourth Edition
52
Understanding Precedence When
Combining AND and OR Selections
(continued)
• When only one of multiple conditions must be true,
use multiple ORs
Programming Logic and Design, Introductory, Fourth Edition
53
Understanding Precedence When
Combining AND and OR Selections
(continued)
• When AND and OR operators are combined in the
same statement, AND operators are evaluated first
• Use parentheses to correct logic and force
evaluations to occur in the order desired
Programming Logic and Design, Introductory, Fourth Edition
54
Understanding Precedence When
Combining AND and OR Selections
(continued)
• Mixing AND and OR operators makes logic more
complicated
• Can avoid mixing AND and OR decisions by
nesting if statements
Programming Logic and Design, Introductory, Fourth Edition
55
Understanding Precedence When
Combining AND and OR Selections
(continued)
Programming Logic and Design, Introductory, Fourth Edition
56
Understanding the Case Structure
• Used to provide a series of alternatives based on
the value of a single variable
• Replaces a series of chained if-else statements
• May make the code easier to read
Programming Logic and Design, Introductory, Fourth Edition
57
Understanding the Case Structure
(continued)
Programming Logic and Design, Introductory, Fourth Edition
58
Understanding the Case Structure
(continued)
Programming Logic and Design, Introductory, Fourth Edition
59
Using Decision Tables
• Managing multiple possible outcomes of multiple
decisions can be difficult
• Decision table: Four-part problem-analysis tool
– Conditions
– Possible combinations of Boolean values for each
condition
– Possible actions based on the conditions
– Specific actions that correspond to each Boolean
value of each condition
Programming Logic and Design, Introductory, Fourth Edition
60
Using Decision Tables (continued)
• Developing the application:
– The data
Programming Logic and Design, Introductory, Fourth Edition
61
Using Decision Tables (continued)
• Rules for assigning residence halls:
– Students under age 21 who request a hall with quiet
study hours: Addams Hall
– Students under age 21 who do not request a hall
with quiet study hours: Grant Hall
– Students age 21 and over who request a hall with
quiet study hours: Lincoln Hall
– Students age 21 and over who do not request a hall
with quiet study hours: Lincoln Hall
Programming Logic and Design, Introductory, Fourth Edition
62
Using Decision Tables (continued)
• Developing the application:
– The desired output
Programming Logic and Design, Introductory, Fourth Edition
63
Using Decision Tables (continued)
Programming Logic and Design, Introductory, Fourth Edition
64
Using Decision Tables (continued)
Programming Logic and Design, Introductory, Fourth Edition
65
Using Decision Tables (continued)
• To create a decision table:
– List all possible conditions
– Determine the possible Boolean value combinations
for each condition
– # combinations = 2 (number of conditions)
Programming Logic and Design, Introductory, Fourth Edition
66
Using Decision Tables (continued)
• To create a decision table (continued):
– Add rows to list possible outcome actions
Programming Logic and Design, Introductory, Fourth Edition
67
Using Decision Tables (continued)
• To create a decision table (continued):
– Choose one required outcome for each combination
Programming Logic and Design, Introductory, Fourth Edition
68
Using Decision Tables (continued)
• Create a flowchart from the decision table
– Draw a path for each column’s outcome
Programming Logic and Design, Introductory, Fourth Edition
69
Using Decision Tables (continued)
• Resulting flowchart created directly from table has
two identical outcomes: an unneeded question
Programming Logic and Design, Introductory, Fourth Edition
70
Using Decision Tables (continued)
Programming Logic and Design, Introductory, Fourth Edition
71
Summary
• Decisions involve evaluating Boolean expressions
• Use relational operators to compare values
• AND decision requires that both conditions be true
to produce a true result
• In an AND decision, first ask the question that is
less likely to be true
• OR decision requires that either of the conditions
be true to produce a true result
Programming Logic and Design, Introductory, Fourth Edition
72
Summary (continued)
• In an OR decision, first ask the question that is more
likely to be true
• For a range check, make comparisons with the
highest or lowest values in each range
• Eliminate unnecessary or previously answered
questions
• Case structure allows a series of alternative actions
based on the value in a single variable
• Decision table aids in program design analysis to
manage multiple conditions and decisions
Programming Logic and Design, Introductory, Fourth Edition
73
Descargar

Programming Logic and Design Fourth Edition, Introductory