An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions Objectives • Evaluate Boolean expressions to make comparisons • Use the relational comparison operators • Make decisions with objects • Understand AND logic • Understand OR logic An Object-Oriented Approach to Programming Logic and Design 2 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 An Object-Oriented Approach to Programming Logic and Design 3 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 singlealternative, or unary selection structure; also called the if-then structure An Object-Oriented Approach to Programming Logic and Design 4 Evaluating Boolean Expressions to Make Comparisons (continued) Example: if the answer to the question is yes, then do something else do somethingElse endif An Object-Oriented Approach to Programming Logic and Design 5 Evaluating Boolean Expressions to Make Comparisons (continued) An Object-Oriented Approach to Programming Logic and Design 6 Evaluating Boolean Expressions to Make Comparisons (continued) Example: An Object-Oriented Approach to Programming Logic and Design 7 Evaluating Boolean Expressions to Make Comparisons (continued) • Boolean expression – one that represents only one of two states, True or False An Object-Oriented Approach to Programming Logic and Design 8 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 An Object-Oriented Approach to Programming Logic and Design 9 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 An Object-Oriented Approach to Programming Logic and Design 10 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 An Object-Oriented Approach to Programming Logic and Design 11 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 An Object-Oriented Approach to Programming Logic and Design 12 Using the Relational Comparison Operators (continued) Example: if customerAge >= 65 then discount = 0.10 else discount = 0 endif if customerAge < 65 then discount = 0 else discount = 0.10 endif An Object-Oriented Approach to Programming Logic and Design 13 Using the Relational Comparison Operators (continued) • Negative comparisons can lead to double negatives – use caution! An Object-Oriented Approach to Programming Logic and Design 14 Using the Relational Comparison Operators (continued) • Rephrase question to eliminate double negative: An Object-Oriented Approach to Programming Logic and Design 15 Using the Relational Comparison Operators (continued) An Object-Oriented Approach to Programming Logic and Design 16 Making Decisions with Objects • Object-oriented programs make decisions about primitive types and objects • Decision-making logic is the same for both An Object-Oriented Approach to Programming Logic and Design 17 Making Decisions about Primitive Data Types Example with primitive data: An Object-Oriented Approach to Programming Logic and Design 18 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 An Object-Oriented Approach to Programming Logic and Design 19 Making Decisions about Objects (continued) An Object-Oriented Approach to Programming Logic and Design 20 Making Decisions about Objects (continued) An Object-Oriented Approach to Programming Logic and Design 21 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 An Object-Oriented Approach to Programming Logic and Design 22 Understanding AND Logic (continued) An Object-Oriented Approach to Programming Logic and Design 23 Understanding AND Logic (continued) An Object-Oriented Approach to Programming Logic and Design 24 Understanding AND Logic (continued) • Use care to match the else statements correctly! An Object-Oriented Approach to Programming Logic and Design 25 Understanding AND Logic (continued) An Object-Oriented Approach to Programming Logic and Design 26 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 An Object-Oriented Approach to Programming Logic and Design 27 Nesting and Decisions for Efficiency (continued) Example: Both produce the same decision An Object-Oriented Approach to Programming Logic and Design 28 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 An Object-Oriented Approach to Programming Logic and Design 29 Combining Decisions in an AND Selection (continued) An Object-Oriented Approach to Programming Logic and Design 30 Avoiding Common Errors in an AND Selection Error: Decisions are not nested! An Object-Oriented Approach to Programming Logic and Design 31 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 Incorrect example: if itemsSold >= 5 AND <= 10 then bonus = 75 endif An Object-Oriented Approach to Programming Logic and Design 32 Understanding OR Logic • OR decision requires that at least one of the questions evaluates to True An Object-Oriented Approach to Programming Logic and Design 33 Writing Or Decisions for Efficiency • In an OR decision, first ask the question that is more likely to be True An Object-Oriented Approach to Programming Logic and Design 34 Combining Decisions in an Or Selection • Logical OR operator allows two or more questions to be asked in a single statement • Most languages require a complete Boolean expression on each side of the OR operator An Object-Oriented Approach to Programming Logic and Design 35 Combining Decisions in an Or Selection (continued) An Object-Oriented Approach to Programming Logic and Design 36 Avoiding Common Errors in an Or Selection Unstructured Example: An Object-Oriented Approach to Programming Logic and Design 37 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 An Object-Oriented Approach to Programming Logic and Design 38 Avoiding Common Errors in an Or Selection (continued) An Object-Oriented Approach to Programming Logic and Design 39 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 An Object-Oriented Approach to Programming Logic and Design 40 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 An Object-Oriented Approach to Programming Logic and Design 41 Using Selection Within Ranges (continued) An Object-Oriented Approach to Programming Logic and Design 42 Using Selection Within Ranges (continued) An Object-Oriented Approach to Programming Logic and Design 43 Using Selection Within Ranges (continued) An Object-Oriented Approach to Programming Logic and Design 44 Understanding Common Errors Using Range Checks • Avoid logical paths that are unreachable • Avoid asking questions that have just one possible answer or outcome An Object-Oriented Approach to Programming Logic and Design 45 Understanding Common Errors Using Range Checks (continued) An Object-Oriented Approach to Programming Logic and Design 46 Understanding Common Errors Using Range Checks (continued) An Object-Oriented Approach to Programming Logic and Design 47 Understanding Common Errors Using Range Checks (continued) An Object-Oriented Approach to Programming Logic and Design 48 Understanding Common Errors Using Range Checks (continued) An Object-Oriented Approach to Programming Logic and Design 49 Understanding Precedence when Combining AND and Or Selections • Most languages allow combination of multiple AND and OR operators in an expression • When AND and OR are in the same statement, the And operator takes precedence • Use parentheses to control which operators are evaluated first, or use nested if statements instead of ANDs and ORs An Object-Oriented Approach to Programming Logic and Design 50 Understanding Precedence when Combining AND and Or Selections (continued) Examples: if age<=12 OR age>=65 AND rating=“G” then print “Discount applies” if (age<=12 OR age>=65) AND rating=“G” then print “Discount applies” An Object-Oriented Approach to Programming Logic and Design 51 Understanding Precedence when Combining And and Or Selections (continued) An Object-Oriented Approach to Programming Logic and Design 52 Understanding the Case Structure • case structure allows a selection between multiple alternatives • Improves the readability of the code, because it replaces “chained” if statements • Tests a variable against a series of values, and executes only the code for that value An Object-Oriented Approach to Programming Logic and Design 53 Understanding the Case Structure (continued) An Object-Oriented Approach to Programming Logic and Design 54 Understanding the Case Structure (continued) An Object-Oriented Approach to Programming Logic and Design 55 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 An Object-Oriented Approach to Programming Logic and Design 56 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) An Object-Oriented Approach to Programming Logic and Design 57 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: An Object-Oriented Approach to Programming Logic and Design 58 Using Decision Tables (continued) • Add rows to list possible outcome actions: An Object-Oriented Approach to Programming Logic and Design 59 Using Decision Tables (continued) • Choose one outcome for each possible combination of conditions: An Object-Oriented Approach to Programming Logic and Design 60 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 An Object-Oriented Approach to Programming Logic and Design 61 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 endif An Object-Oriented Approach to Programming Logic and Design 62 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 An Object-Oriented Approach to Programming Logic and Design 63 Using Decision Tables (continued) An Object-Oriented Approach to Programming Logic and Design 64 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 An Object-Oriented Approach to Programming Logic and Design 65 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 An Object-Oriented Approach to Programming Logic and Design 66 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 An Object-Oriented Approach to Programming Logic and Design 67
© Copyright 2026 Paperzz