Download presentation
Presentation is loading. Please wait.
Published byKatherine Hampton Modified over 9 years ago
1
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions
2
Programming Logic and Design, Introductory, Fourth Edition2 Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Understand AND logic Understand OR logic Use selections within ranges
3
Programming Logic and Design, Introductory, Fourth Edition3 Objectives (continued) Understand precedence when combining AND and OR selections Understand the case structure Use decision tables
4
Programming Logic and Design, Introductory, Fourth Edition4 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
5
Programming Logic and Design, Introductory, Fourth Edition5 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
6
Programming Logic and Design, Introductory, Fourth Edition6 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: >= –Not equal: <>
7
Programming Logic and Design, Introductory, Fourth Edition7 Using the Relational Comparison Operators (continued)
8
Programming Logic and Design, Introductory, Fourth Edition8 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
9
Programming Logic and Design, Introductory, Fourth Edition9 Evaluating Boolean Expressions to Make Comparisons Dual-alternative (or binary) selection structure: – Provides an action for each of two possible outcomes
10
Programming Logic and Design, Introductory, Fourth Edition10 Evaluating Boolean Expressions to Make Comparisons (continued) Dual-alternative (or binary) selection structure: – Also called an if-then-else structure
11
Programming Logic and Design, Introductory, Fourth Edition11 Evaluating Boolean Expressions to Make Comparisons (continued) Single-alternative (or unary) selection structure –Action is provided for only one outcome
12
Programming Logic and Design, Introductory, Fourth Edition12 Evaluating Boolean Expressions to Make Comparisons (continued) Single-alternative (or unary) selection structure –Also called an if-then structure
13
Programming Logic and Design, Introductory, Fourth Edition13 IF customerAge >= 65 THEN discount = 0.10 ELSE discount = 0 ENDIF IF customerAge < 65 THEN discount = 0 ELSE discount = 0.10 ENDIF IF customerAge < 64 THEN discount = 0 ELSE discount = 0.10 ENDIF Using the Relational Comparison Operators (continued) Each calculates a discount of 10% only when the customer age is 65 years old or greater ALL ARE CORRECT – THERE ARE NO WRONG WAYS!
14
Programming Logic and Design, Introductory, Fourth Edition14 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
15
Programming Logic and Design, Introductory, Fourth Edition15 Using the Relational Comparison Operators (continued) Rephrase in the positive Some comparisons are clearer when negative is used
16
Programming Logic and Design, Introductory, Fourth Edition16 Understanding AND Logic AND decision –Requires that both (ALL) of two tests evaluate to True –Requires a nested decision (nested if)
17
Programming Logic and Design, Introductory, Fourth Edition17 Understanding AND Logic (continued) Developing the application –The input data
18
Programming Logic and Design, Introductory, Fourth Edition18 Understanding AND Logic (continued) Developing the application –The intended output:
19
Programming Logic and Design, Introductory, Fourth Edition19 Understanding AND Logic (continued) Developing the application –The mainline logic:
20
Programming Logic and Design, Introductory, Fourth Edition20 Understanding AND Logic (continued) housekeeping() module:
21
Programming Logic and Design, Introductory, Fourth Edition21 Understanding AND Logic (continued) createReport() module:
22
Programming Logic and Design, Introductory, Fourth Edition22 Understanding AND Logic (continued)
23
Programming Logic and Design, Introductory, Fourth Edition23 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
24
Programming Logic and Design, Introductory, Fourth Edition24 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 (once for each employee) –Second question is asked 500 times ( there are only 500 in dental plan therefore only 500 “yes” answers) YES path NO path and completion of YES path YES path
25
Programming Logic and Design, Introductory, Fourth Edition25 Writing Nested AND Decisions for Efficiency (continued) Rule of Thumb: First ask the question that is less likely to be true – more likely to be false (first false terminates the question) –Reduces the number of times the second question will need to be asked
26
Programming Logic and Design, Introductory, Fourth Edition26 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
27
Programming Logic and Design, Introductory, Fourth Edition27 Combining Decisions in an AND Selection (continued) Single IF statement
28
Programming Logic and Design, Introductory, Fourth Edition28 Combining Decisions in an AND Selection (continued) Multiple IF statement
29
Programming Logic and Design, Introductory, Fourth Edition29 Avoiding Common Errors in an AND Selection Failure to nest 2 nd decision entirely within 1 st decision Tests each decision separately no matter the outcome of the other decision.
30
Programming Logic and Design, Introductory, Fourth Edition30 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
31
Programming Logic and Design, Introductory, Fourth Edition31 Understanding OR Logic (continued) createReport() module: IF empMedicalIns = Y OR empDentalIns = “Y” THEN print empIdNumber, empLastName, empFirstName ENDIF
32
Programming Logic and Design, Introductory, Fourth Edition32 Avoiding Common Errors in an OR Selection (continued) Incorrect interpretation of English: –Casual use of AND when logic requires OR
33
Programming Logic and Design, Introductory, Fourth Edition33 Avoiding Common Errors in an OR Selection (continued) Correct logic:
34
Programming Logic and Design, Introductory, Fourth Edition34 Avoiding Common Errors in an OR Selection (continued) Incorrect interpretation of English –Use of OR when AND logic is required < 65: All patrons (including 12 and below) > 12: All patrons (including 65 and above)
35
Programming Logic and Design, Introductory, Fourth Edition35 Avoiding Common Errors in an OR Selection (continued) Correct logic:
36
Programming Logic and Design, Introductory, Fourth Edition36 Writing OR Decisions for Efficiency How many decisions?
37
Programming Logic and Design, Introductory, Fourth Edition37 Writing OR Decisions for Efficiency (continued)
38
Programming Logic and Design, Introductory, Fourth Edition38 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 (first true will generate the TRUE path and the rest of the ORs will be ignored)
39
Programming Logic and Design, Introductory, Fourth Edition39 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
40
Programming Logic and Design, Introductory, Fourth Edition40 Combining Decisions in an OR Selection (continued) Using an OR operator:
41
Programming Logic and Design, Introductory, Fourth Edition41 Combining Decisions in an OR Selection (continued) What the computer actually does:
42
Programming Logic and Design, Introductory, Fourth Edition42 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
43
Programming Logic and Design, Introductory, Fourth Edition43 Range Selection Range: 10 -11 to get TRUE Task: print if empRate is 10 or 11
44
Programming Logic and Design, Introductory, Fourth Edition44 SYNTAX error Range Selection Good
45
Programming Logic and Design, Introductory, Fourth Edition45 Using Selections Within Ranges (continued) Using high-end values in the range check:
46
Programming Logic and Design, Introductory, Fourth Edition46 Using Selections Within Ranges (continued) Using low-end values in the range check:
47
Programming Logic and Design, Introductory, Fourth Edition47 Common Errors Using Range Checks (continued) Unnecessary
48
Programming Logic and Design, Introductory, Fourth Edition48 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
49
Programming Logic and Design, Introductory, Fourth Edition49 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
50
Programming Logic and Design, Introductory, Fourth Edition50 Understanding Precedence When Combining AND and OR Selections (continued) AND OR
51
Programming Logic and Design, Introductory, Fourth Edition51 Understanding the Case Structure Used to provide a series of alternatives (IF statements) based on the value of a single variable Replaces a series of chained if-else statements May make the code easier to read
52
Programming Logic and Design, Introductory, Fourth Edition52 Understanding the Case Structure (continued)
53
Programming Logic and Design, Introductory, Fourth Edition53 Understanding the Case Structure (continued)
54
Programming Logic and Design, Introductory, Fourth Edition54 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
55
Programming Logic and Design, Introductory, Fourth Edition55 Using Decision Tables (continued) Developing the application: –The data
56
Programming Logic and Design, Introductory, Fourth Edition56 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
57
Programming Logic and Design, Introductory, Fourth Edition57 Using Decision Tables (continued) Developing the application: –The desired output
58
Programming Logic and Design, Introductory, Fourth Edition58 Using Decision Tables (continued)
59
Programming Logic and Design, Introductory, Fourth Edition59 Using Decision Tables (continued)
60
Programming Logic and Design, Introductory, Fourth Edition60 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) –# of possible outcomes = 4 (residence halls) Addams Hall Grant Hall Lincoln Hall
61
Programming Logic and Design, Introductory, Fourth Edition61 Using Decision Tables (continued) To create a decision table (continued): –Add rows to list possible outcome actions
62
Programming Logic and Design, Introductory, Fourth Edition62 Using Decision Tables (continued) To create a decision table (continued): –Choose one required outcome for each combination
63
Programming Logic and Design, Introductory, Fourth Edition63 Using Decision Tables (continued) Create a flowchart from the decision table –Draw a path for each column’s outcome
64
Programming Logic and Design, Introductory, Fourth Edition64 Using Decision Tables (continued) Resulting flowchart created directly from table has two identical outcomes: an unneeded question
65
Programming Logic and Design, Introductory, Fourth Edition65 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
66
Programming Logic and Design, Introductory, Fourth Edition66 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.