Download presentation
Presentation is loading. Please wait.
Published byJudith Johns Modified over 9 years ago
1
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions
2
An Object-Oriented Approach to Programming Logic and Design 2 Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Make decisions with objects Understand AND logic Understand OR logic
3
An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Use selections within ranges Understand common errors using range checks Understand precedence when combining AND and OR selections Understand the case structure Use a decision table
4
An Object-Oriented Approach to Programming Logic and Design 4 Evaluating Boolean Expressions to Make Comparisons People think computers are smart because computers can make decisions Dual-alternative, or binary, selection structure: logic flows to one of two alternatives; also called the if-then-else structure If no else clause, it is called the single- alternative, or unary selection structure; also called the if-then structure
5
An Object-Oriented Approach to Programming Logic and Design 5 Evaluating Boolean Expressions to Make Comparisons (continued) Example: if the answer to the question is yes, then do something else do somethingElse endif
6
An Object-Oriented Approach to Programming Logic and Design 6 Evaluating Boolean Expressions to Make Comparisons (continued)
7
An Object-Oriented Approach to Programming Logic and Design 7 Evaluating Boolean Expressions to Make Comparisons (continued) Example:
8
An Object-Oriented Approach to Programming Logic and Design 8 Evaluating Boolean Expressions to Make Comparisons (continued) Boolean expression – one that represents only one of two states, True or False
9
An Object-Oriented Approach to Programming Logic and Design 9 Using the Relational Comparison Operators Usually you compare values of the same type Three possible comparison decisions: –Both values are equal –First value is greater than second value –First value is less than second value Values to be compared can be variables or constants
10
An Object-Oriented Approach to Programming Logic and Design 10 Using the Relational Comparison Operators (continued) Each programming language has its own set of relational comparison operators, or comparison symbols Most languages allow the > and < signs for comparison Comparison operators require a value on each side of the operator
11
An Object-Oriented Approach to Programming Logic and Design 11 Using the Relational Comparison Operators (continued) Most languages support: –>= First value is greater than or equal to the second value –<= First value is less than or equal to the second value –The two values are not equal While not required, these comparisons simplify the code
12
An Object-Oriented Approach to Programming Logic and Design 12 Using the Relational Comparison Operators (continued) If a >= b is True, then a < b is False If a >= b is False, then a < b is True Rephrase the question and swap the action taken to make the same decision
13
An Object-Oriented Approach to Programming Logic and Design 13 Using the Relational Comparison Operators (continued) Example: if customerAge < 65 then discount = 0 else discount = 0.10 endif if customerAge >= 65 then discount = 0.10 else discount = 0 endif
14
An Object-Oriented Approach to Programming Logic and Design 14 Using the Relational Comparison Operators (continued) Negative comparisons can lead to double negatives – use caution!
15
An Object-Oriented Approach to Programming Logic and Design 15 Using the Relational Comparison Operators (continued ) Rephrase question to eliminate double negative:
16
An Object-Oriented Approach to Programming Logic and Design 16 Using the Relational Comparison Operators (continued)
17
An Object-Oriented Approach to Programming Logic and Design 17 Making Decisions with Objects Object-oriented programs make decisions about primitive types and objects Decision-making logic is the same for both
18
An Object-Oriented Approach to Programming Logic and Design 18 Making Decisions about Primitive Data Types Example with primitive data:
19
An Object-Oriented Approach to Programming Logic and Design 19 Making Decisions about Objects Decision making with objects may be more complex because the data is usually hidden Object instance names are references, or pointers, to the object Comparing pointers only determines if they point to the same or different memory locations; does NOT compare the value of the instance data You must compare the object’s data values
20
An Object-Oriented Approach to Programming Logic and Design 20 Making Decisions about Objects (continued)
21
An Object-Oriented Approach to Programming Logic and Design 21 Making Decisions about Objects (continued)
22
An Object-Oriented Approach to Programming Logic and Design 22 Understanding AND Logic You may need more than one selection structure to make a decision AND decision requires that both questions evaluate as True Also called a compound decision Requires a nested decision – a nested if
23
An Object-Oriented Approach to Programming Logic and Design 23 Understanding AND Logic (continued)
24
An Object-Oriented Approach to Programming Logic and Design 24 Understanding AND Logic (continued)
25
An Object-Oriented Approach to Programming Logic and Design 25 Understanding AND Logic (continued) Use care to match the else statements correctly!
26
An Object-Oriented Approach to Programming Logic and Design 26 Understanding AND Logic (continued)
27
An Object-Oriented Approach to Programming Logic and Design 27 Nesting and Decisions for Efficiency In an AND decision, either decision can come first (if a single action results) Efficiency may be improved by making a good choice for which decision comes first The choice may be based on the data: What is the outcome that is expected most often? Rule of thumb: first ask the question that is less likely to be true – may eliminate the need to evaluate the second question
28
An Object-Oriented Approach to Programming Logic and Design 28 Nesting and Decisions for Efficiency (continued) Example: Both produce the same decision
29
An Object-Oriented Approach to Programming Logic and Design 29 Combining Decisions in an AND Selection Most languages allow multiple comparisons using a logical AND operator Logical AND is equivalent to nested if statements Consider the order of the questions for efficiency
30
An Object-Oriented Approach to Programming Logic and Design 30 Combining Decisions in an AND Selection (continued)
31
An Object-Oriented Approach to Programming Logic and Design 31 Avoiding Common Errors in an AND Selection Error: Decisions are not nested!
32
An Object-Oriented Approach to Programming Logic and Design 32 Avoiding Common Errors in an AND Selection (continued) Using AND to handle a range of values requires two complete Boolean expressions Correct example: if itemsSold >= 5 AND itemsSold <= 10 then bonus = 75 endif if itemsSold >= 5 AND <= 10 then bonus = 75 endif Incorrect example:
33
An Object-Oriented Approach to Programming Logic and Design 33 Understanding OR Logic OR TrueOR decision requires that at least one of the questions evaluates to True
34
An Object-Oriented Approach to Programming Logic and Design 34 Writing Or Decisions for Efficiency ORIn an OR decision, first ask the question that is more likely to be True
35
An Object-Oriented Approach to Programming Logic and Design 35 Combining Decisions in an Or Selection ORLogical OR operator allows two or more questions to be asked in a single statement ORMost languages require a complete Boolean expression on each side of the OR operator
36
An Object-Oriented Approach to Programming Logic and Design 36 Combining Decisions in an Or Selection (continued)
37
An Object-Oriented Approach to Programming Logic and Design 37 Avoiding Common Errors in an Or Selection Unstructured Example:
38
An Object-Oriented Approach to Programming Logic and Design 38 Avoiding Common Errors in an Or Selection (continued) Warning! Casual use of English may use “and” and “or” incorrectly Be sure to state required decisions clearly and unambiguously when there are multiple decisions
39
An Object-Oriented Approach to Programming Logic and Design 39 Avoiding Common Errors in an Or Selection (continued)
40
An Object-Oriented Approach to Programming Logic and Design 40 Avoiding Common Errors in an Or Selection (continued) Most languages support a NOT operator Logical NOT operator reverses the meaning of a Boolean expression NOT operator is unary – place it in front of a single expression Example: if NOT (age < 21) then print “OK” endif
41
An Object-Oriented Approach to Programming Logic and Design 41 Using Selection Within Ranges A range check compares a variable to a series of values between limits Make comparisons using either the lowest or highest value in each range of values
42
An Object-Oriented Approach to Programming Logic and Design 42 Using Selection Within Ranges (continued)
43
An Object-Oriented Approach to Programming Logic and Design 43 Using Selection Within Ranges (continued)
44
An Object-Oriented Approach to Programming Logic and Design 44 Using Selection Within Ranges (continued)
45
An Object-Oriented Approach to Programming Logic and Design 45 Understanding Common Errors Using Range Checks Avoid logical paths that are unreachable Avoid asking questions that have just one possible answer or outcome
46
An Object-Oriented Approach to Programming Logic and Design 46 Understanding Common Errors Using Range Checks (continued)
47
An Object-Oriented Approach to Programming Logic and Design 47 Understanding Common Errors Using Range Checks (continued)
48
An Object-Oriented Approach to Programming Logic and Design 48 Understanding Common Errors Using Range Checks (continued)
49
An Object-Oriented Approach to Programming Logic and Design 49 Understanding Common Errors Using Range Checks (continued)
50
An Object-Oriented Approach to Programming Logic and Design 50 Understanding Precedence when Combining AND and Or Selections ORMost languages allow combination of multiple AND and OR operators in an expression ORWhen AND and OR are in the same statement, the And operator takes precedence ORUse parentheses to control which operators are evaluated first, or use nested if statements instead of ANDs and ORs
51
An Object-Oriented Approach to Programming Logic and Design 51 Understanding Precedence when Combining AND and Or Selections (continued) Examples: if age =65 AND rating=“G” then print “Discount applies” if (age =65) AND rating=“G” then print “Discount applies”
52
An Object-Oriented Approach to Programming Logic and Design 52 Understanding Precedence when Combining And and Or Selections (continued)
53
An Object-Oriented Approach to Programming Logic and Design 53 Understanding the Case Structure case structure allows a selection between multiple alternatives Improves the readability of the code, because it replaces “chained” if statements T ests a variable against a series of values, and executes only the code for that value
54
An Object-Oriented Approach to Programming Logic and Design 54 Understanding the Case Structure (continued)
55
An Object-Oriented Approach to Programming Logic and Design 55 Understanding the Case Structure (continued)
56
An Object-Oriented Approach to Programming Logic and Design 56 Using Decision Tables Decision table – a problem analysis tool consisting of: –Conditions –Possible combinations of Boolean values for the conditions –Possible actions based on the outcomes –A specific action corresponding to each Boolean value of each condition
57
An Object-Oriented Approach to Programming Logic and Design 57 Using Decision Tables (continued) Example: College dorm assignment rules: Students under age 21 who request a quiet dorm are assigned to Addams Hall Students under age 21 who do not request a quiet dorm are assigned to Grant Hall Students 21 and over who request a quiet dorm are assigned to Lincoln Hall Students 21 and over who do not request a quiet dorm are assigned to Lincoln Hall (the only dorm for over 21)
58
An Object-Oriented Approach to Programming Logic and Design 58 Using Decision Tables (continued) List all possible conditions that affect the outcome: –Age is under 21, or not –Requests quiet dorm, or not Determine all possible Boolean combinations for the conditions:
59
An Object-Oriented Approach to Programming Logic and Design 59 Using Decision Tables (continued) Add rows to list possible outcome actions:
60
An Object-Oriented Approach to Programming Logic and Design 60 Using Decision Tables (continued) Choose one outcome for each possible combination of conditions:
61
An Object-Oriented Approach to Programming Logic and Design 61 Using Decision Tables (continued) Write pseudocode to describe the first and second columns for which age is less than 21: if age <21 then if quietRequest = “Y” then assignedHall = “Addams” else assignedHall = “Grant” endif
62
An Object-Oriented Approach to Programming Logic and Design 62 Using Decision Tables (continued) Now add pseudocode to describe the third and fourth columns for which age is greater than or equal to 21: else if quietRequest = “Y” then assignedHall = “Lincoln” else assignedHall = “Lincoln” endif
63
An Object-Oriented Approach to Programming Logic and Design 63 Using Decision Tables (continued) But the state of “quietRequest” does not matter if the student is >= 21, so there is no need to test the “quietRequest” variable: else assignedHall = “Lincoln” endif
64
An Object-Oriented Approach to Programming Logic and Design 64 Using Decision Tables (continued)
65
An Object-Oriented Approach to Programming Logic and Design 65 Summary All decisions evaluate Boolean expressions Any two values can be compared using relational comparison operators AND operator allows two or more conditions to be tested in a single statement AND decision requires that both conditions are true in order to return a True result In an AND decision, first ask the question that is less likely to be true
66
An Object-Oriented Approach to Programming Logic and Design 66 Summary (continued) OR decision requires that only one of the two conditions is true to return a True result In an OR decision, first ask the question that is more likely to be true For range checks, make comparisons with either the lowest or highest value in each range
67
An Object-Oriented Approach to Programming Logic and Design 67 Summary (continued) Avoid unnecessary or previously answered questions case structure allows question with more than two alternatives Decision table lists conditions and combinations of outcomes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.