Computing with Business Applications
1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning.
2: VBA for Modelers: Developing Decision Support Systems with Microsoft Office Excel; by Albright 5th edition, South Western Cengage Learning.
Chapter 4
Decision-Making
1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning.
pages: 125-165
Objectives
In this chapter, you will learn about:
• 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
3© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Boolean Expressions and the Selection Structure
• Boolean expressions can be only true or false
• Every computer decision yields a true-or-false, yes-or-no, 1-or-0 result
• Used in every selection structure
4© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Boolean Expressions and the Selection Structure (continued)
• Dual-alternative (or binary) selection structure • Provides an action for each of two possible outcomes
5
Figure 4-1 The dual-alternative selection structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Boolean Expressions and the Selection Structure (continued)
• Single-alternative (or unary) selection structure • Action is provided for only one outcome
• if-then
6
Figure 4-2 The single-alternative selection structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
7
Figure 4-3 Flowchart and pseudocode for overtime payroll program
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
8
Figure 4-3 Flowchart and pseudocode for overtime payroll program (continued)
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Boolean Expressions and the Selection Structure (continued)
• if-then-else decision • if-then clause
• Holds the action or actions that execute when the tested condition in the decision is true
• else clause • Executes only when the tested condition in the decision is false
9© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using Relational Comparison Operators • Relational comparison operators
– Six types supported by all modern programming languages
– Two values compared can be either variables or constants
• Trivial expressions – Will always evaluate to the same result
– Examples: • true for 20 = 20?
• false for 30 = 40?
10© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
11
Table 4-1 Relational comparison operators
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using Relational Comparison Operators (continued)
• Any decision can be made with only three types of comparisons: =, >, and < – The >= and <= operators are not necessary but make code more readable
• “Not equal” operator • Involves thinking in double negatives
• Best to restrict usage to “if without an else”—that is, only take action when some comparison is false
12© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using Relational Comparison Operators (continued)
13
Figure 4-5 Using a negative comparison
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using Relational Comparison Operators (continued)
14
Figure 4-6 Using the positive equivalent of the negative comparison in Figure 4-5
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding a Common Error with Relational Operators
• Common errors • Using the wrong operator
• Think BIG > small
• Think small < BIG
• Missing the boundary or limit required for a selection
15© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding AND Logic • Compound condition
• Asks multiple questions before an outcome is determined
• AND decision • Requires that both of two tests evaluate to true
• Requires a nested decision (nested if) or a cascading if statement
16© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
17
Figure 4-7 Flowchart and pseudocode for cell phone billing program
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
18
Figure 4-7 Flowchart and pseudocode for cell phone billing program (continued)
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Nesting AND Decisions for Efficiency
• When nesting decisions • Either selection can come first
• Performance time can be improved by asking questions in the proper order
• In an AND decision, first ask the question that is less likely to be true • Eliminates as many instances of the second decision as possible
• Speeds up processing time
19© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using the AND Operator
• Conditional AND operator • Ask two or more questions in a single comparison
• Each Boolean expression must be true for entire expression to evaluate to true
• Truth tables • Describe the truth of an entire expression based on the truth of its parts
• Short-circuit evaluation • Expression evaluated only as far as necessary to determine truth
20© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using the AND Operator (continued)
21
Table 4-2 Truth table for the AND operator
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
22
Figure 4-9 Using an AND operator and the logic behind it
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors in an AND Selection • Second decision must be made entirely within the first decision
• In most programming languages, logical AND is a binary operator • Requires a complete Boolean expression on both sides
23© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding OR Logic
• OR decision • Take action when one or the other of two conditions is true
• Example • “Are you free for dinner Friday or Saturday?”
24© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Writing OR Decisions for Efficiency
• May ask either question first • Both produce the same output but vary widely in number of questions asked
• If first question is true, no need to ask second
• In an OR decision, first ask the question that is more likely to be true • Eliminate as many extra decisions as possible
25© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using the OR Operator
• Conditional OR operator • Ask two or more questions 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 • Consider efficiency
• Computer can ask only one question at a time
26© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using the OR Operator (continued)
27
Table 4-3 Truth table for the OR operator
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
28
Figure 4-13 Using an OR operator and the logic behind it
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors in an OR Selection
• Second question must be a self-contained structure with one entry and exit point
• Request for A and B in English logically means a request for A or B • Example
• “Add $20 to the bill of anyone who makes more than 100 calls and to anyone who has used more than 500 minutes”
• “Add $20 to the bill of anyone who has made more than 100 calls or has used more than 500 minutes”
29© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors in an OR Selection (continued)
30
Figure 4-14 Unstructured flowchart for determining customer cell phone bill
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Both yes and no take you to the same place
Avoiding Common Errors in an OR Selection (continued)
31
Figure 4-15 Incorrect logic that attempts to provide a discount for young and old movie patrons
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors in an OR Selection (continued)
32
Figure 4-16 Correct logic that provides a discount for young and old movie patrons
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors in an OR Selection (continued)
33
Figure 4-17 Incorrect logic that attempts to charge full price for patrons whose age is over 12 and under 65
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors in an OR Selection (continued)
34
Figure 4-18 Correct logic that charges full price for patrons whose age is over 12 and under 65
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Making 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 >
35© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Making Selections within Ranges (continued)
36
Figure 4-19 Discount rates based on items ordered
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
37
Figure 4-20 Flowchart and pseudocode of logic that selects correct discount based on items
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Avoiding Common Errors When Using Range Checks
• Avoid a dead or unreachable path • Don’t check for values that can never occur
• Requires some prior knowledge of the data
• Never ask a question if there is only one possible outcome
• Avoid asking a question when the logic has already determined the outcome
38© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding Precedence When Combining AND and OR Operators
• Combine multiple AND and OR operators in an expression
• When multiple conditions must all be true, use multiple ANDs
if score1 >= MIN_SCORE AND score2 >= MIN_SCORE AND score
3 >= MIN_SCORE then
classGrade = “Pass”
else
classGrade = “Fail”
endif
39© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding Precedence When Combining AND and OR Operators (cont’d)
• When only one of multiple conditions must be true, use multiple ORs
if score1 >= MIN_SCORE OR score2 >= MIN_SCORE OR score3
>= MIN_SCORE then
classGrade = “Pass”
else
classGrade = “Fail”
endif
40© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding Precedence When Combining AND and OR Operators (cont’d)
• When AND and OR operators are combined in the same statement, AND operators are evaluated first if age <= 12 OR age >= 65 AND rating = “G”
• Use parentheses to correct logic and force evaluations to occur in the order desired
if (age <= 12 OR age >= 65) AND rating = “G”
41© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding Precedence When Combining AND and OR Operators (cont’d)
• Mixing AND and OR operators makes logic more complicated
• Can avoid mixing AND and OR decisions by nesting if statements
42© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
43
Figure 4-23 Nested decisions that determine movie patron discount
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
• Decisions involve evaluating Boolean expressions
• Use relational operators to compare values
• An 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
• An OR decision requires that either of the conditions be true to produce a true result
44© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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
• The AND operator takes precedence over the OR operator
45© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part