Chapter 4 Making Decisions At a Glance Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms Lecture Notes Overview Chapter 4 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. CASE structures are discussed in greater depth. Finally, students are introduced to the use of decision tables as an aid for designing programs. Chapter Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections within ranges Learn about precedence when combining AND and OR selections Learn more about the case structure Use a decision table 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 4-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 4-4, and contrast it with the previous figure. 5. Introduce the concept of the Boolean expression. Using the Relational Comparison Operators 1. Discuss the three possible ways to compare two values: a. The two values are equal b. The first value is greater than the second value c. The first value is less than the second value 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 best method of deciding what type of comparison to use in a relational situation. Illustrate with some examples, such as the one on page 141. 6. Point out the code readability issues that occur when using a “not equal” test results in a double negative, and describe how to rephrase the question to have a positive test. 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 action is to be taken only if the decision expression is true 3. What values can result from evaluating Boolean expressions? Answer: True or False Understanding AND Logic 1. Introduce compound conditions, and explain that one type of compound condition is needed when the results of at least two decisions must be true for some action to occur. 2. Introduce the concept of an AND decision, in which both tests must evaluate to True, and explain that AND decisions can be constructed using a nested decision. 3. Describe the nesting of both the flowchart symbols and the pseudocode, and point out that the if statements are nested. 4. Walk through the flowchart in Figure 4-7. Nesting AND Decisions for Efficiency 1. Discuss the analysis one should perform to determine if the order of nesting the decisions will affect performance. 2. Walk through Figure 4-9 to design a nested decision structure that assigns a bonus to salespeople based on their sales. 3. Point out that some prior knowledge of the nature of the data is required to make an analysis. 4. Point out that in some cases, you may not be able to change the order of the questions in an AND statement due to dependencies. 5. Give the general rule that in an AND decision, first ask the question that is less likely to be true, and discuss how this improves efficiency. Combining Decisions in an AND Selection 1. Introduce the concept of using a conditional AND operator to combine two or more questions in a single statement. 2. Point out that all Boolean expressions must be true to return a value of true for the entire statement. 3. Introduce truth tables, and walk through Table 4-2 to describe the truth table for the AND operator. 4. Describe how the computer actually evaluates an expression with a logical AND operator as if it was nested if statements. Then introduce short-circuit evaluation. Avoiding Common Errors in an AND Selection 1. Describe the mistake of failing to nest the second decision entirely within the first decision in an AND selection, and walk through Figure 4-11 to illustrate. 2. Describe the mistake of using incorrect questions to determine inclusion in a range. 3. Describe the failure to use two complete Boolean expressions when using the AND operator. 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 in an OR decision, there is no need to test the second condition. 3. Walk through the sample application shown in Figure 4-12. Writing OR Decisions for Efficiency 1. Walk through Figure 4-13 and discuss how the efficiency is affected by the order in which the questions are asked in an OR statement. 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 conditional OR operator to combine two or more questions in a single statement. 2. Walk through Table 4-3 to present the truth table for the OR operator. 3. Emphasize that only one of the Boolean expressions must be true to return a value of true for the entire statement. 4. Emphasize that the computer can only ask one question at a time, and use Figure 4-14 to illustrate this point. Avoiding Common Errors in an OR Selection 1. Describe the error of creating an OR selection that is unstructured, as shown in Figure 415. 2. Discuss the problems that casual use of the word AND can cause when the logic requires an OR decision. 3. Point out the mistake of incorrectly setting the boundary conditions on an OR decision, as in Figure 4-18. 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 Making Selections within Ranges 1. Introduce the concept of a range check. 2. Point out the need to adjust the question logic for using highest or lowest values by comparing Figures 5-31 and 5-32. Understanding Common Errors Using Range Checks 1. Discuss the error of testing values (using Figure 4-23) that lead to dead or unreachable paths. 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 the OR expression to be evaluated first. 5. Point out that using too many AND and OR operators in a single statement will make the code less readable. The Case Structure 1. Review 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. 3. Contrast Figures 4-26 and 4-27 for simplicity and readability, and remind students of the advantages of using the case structure. Using Decision Tables 1. Introduce the concept of decision tables, and walk through the bulleted list on page 173 that itemizes the four parts of the decision table. 2. Walk through the application example, showing students how to construct a decision table. 3. Describe the process of assigning a single required outcome for each combination. Point out that this tabular approach also helps to ensure that all branches of the logic for all conditions are handled properly. 4. Describe the process of creating a flowchart from a decision table, emphasizing the requirement to create a path for each column’s outcome. 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 4. Name the four parts of a decision table. Answer: conditions, combinations of Boolean values, possible actions, specific actions for each Boolean value of each condition Class Discussion Topics 1. Discuss some examples of casual (and incorrect) use of AND and OR decisions. As a programmer, how might you ensure that the problem is well defined before you start coding? 2. Discuss the use of decision tables. While simple sets of decision conditions can be easily developed, what happens to the number of combinations of Boolean values when the number of conditions increases? Additional Resources 1. An article about truth tables: www.math.csusb.edu/notes/logic/lognot/node1.html 2. A Wikipedia article about decision tables: http://en.wikipedia.org/wiki/Decision_table 3. Preparing a decision table: www.saintmarys.edu/~psmith/417lab3b.html 4. A brief biography of George Boole: www.sjsu.edu/depts/Museum/boole.html Key Terms AND decision—A decision in which two conditions must both be true for an action to take place. Boolean expression—An expression that represents only one of two states, usually expressed as true or false. cascading if statement—A series of nested if statements. case structure—A structure that provides a convenient alternative to using a series of decisions when you must make choices based on the value stored in a single variable. compound condition—A condition in which multiple questions are needed before determining an outcome. conditional AND operator—A symbol that you use to combine decisions so that two (or more) conditions must be true for an action to occur. Also called an AND operator. conditional OR operator—A symbol that you use to combine decisions when any one condition can be true for an action to occur. Also called an OR operator. dead path—A logical path that can never be traveled. Also called an unreachable path. decision table—A problem-analysis tool that lists conditions, Boolean combinations of outcomes when those conditions are tested, and possible actions based on the outcomes. default value—A value assigned after all test conditions are found to be false. else clause—A part of a decision that holds the action or actions that execute only when the Boolean expression in the decision is false. Contrast with if clause. if clause—A part of a decision that holds the action that results when a Boolean expression in a decision is true. Contrast with else clause. if-then—A structure similar to an if-then-else, but no alternative or “else” action is necessary. logical NOT operator—A symbol that reverses the meaning of a Boolean expression. nested decision—A decision “inside of” another decision. Also called a nested if. precedence—The quality of an operation that determines its order of operation or evaluation with others. range check—Comparing a variable to a series of values that mark the limiting ends of ranges. range of values—A set of contiguous values. relational comparison operator—A symbol that expresses Boolean comparisons. Examples include =, >, <, >=, <=, and <>. These operators are also called relational operators or comparison operators. short-circuit evaluation—A logical feature in which expressions in each part of a larger expression are evaluated only as far as necessary to determine the final outcome. trivial—Description of a Boolean expression that always evaluates to the same result. truth table—A diagram used in mathematics and logic to help describe the truth of an entire expression based on the truth of its parts. unreachable path—A logical path that can never be traveled. Also called a dead path.