1 / 49

Programming Logic and Design Fifth Edition, Comprehensive

Programming Logic and Design, Fifth Edition, Comprehensive. 2. Objectives. Evaluate Boolean expressions to make comparisonsUse the relational comparison operatorsLearn about AND logicLearn about OR logicMake selections within ranges. Programming Logic and Design, Fifth Edition, Comprehensive. 3.

marla
Télécharger la présentation

Programming Logic and Design Fifth Edition, Comprehensive

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions

    2. Programming Logic and Design, Fifth Edition, Comprehensive 2 Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections within ranges

    3. Programming Logic and Design, Fifth Edition, Comprehensive 3 Objectives (continued) Learn about precedence when combining AND and OR selections Learn more about the case structure Use a decision table

    4. Programming Logic and Design, Fifth Edition, Comprehensive 4 Evaluating Boolean Expressions to Make Comparisons Dual-alternative (or binary) selection structure: Provides an action for each of two possible outcomes

    5. Programming Logic and Design, Fifth Edition, Comprehensive 5 Evaluating Boolean Expressions to Make Comparisons (continued) Single-alternative (or unary) selection structure Action is provided for only one outcome

    6. Programming Logic and Design, Fifth Edition, Comprehensive 6 Evaluating Boolean Expressions to Make Comparisons (continued)

    7. Programming Logic and Design, Fifth Edition, Comprehensive 7 Evaluating Boolean Expressions to Make Comparisons (continued)

    8. Programming Logic and Design, Fifth Edition, Comprehensive 8 Evaluating Boolean Expressions to Make Comparisons (continued) Boolean expression Represents only one of two states Evaluates to true or false Every decision in a computer program involves evaluating a Boolean expression Computer circuitry consists of two-state on-off switches Represented by 1 or 0 Every computer decision yields a true-false result

    9. Programming Logic and Design, Fifth Edition, Comprehensive 9 Using the Relational Comparison Operators For any two values, three types of comparisons: Two values are equal First value greater than the second value First value less than the second value Relational comparison operators Express Boolean tests Different languages use different symbols

    10. Programming Logic and Design, Fifth Edition, Comprehensive 10 Using the Relational Comparison Operators (continued) Any logical situation can be expressed with only three types of comparisons: =, >, and < Operators >= and <= are not necessary, but make code more readable “Not equal” operator Most confusing of comparisons Most likely to be different in different languages

    11. Programming Logic and Design, Fifth Edition, Comprehensive 11 Using the Relational Comparison Operators (continued)

    12. Programming Logic and Design, Fifth Edition, Comprehensive 12 Using the Relational Comparison Operators (continued)

    13. Programming Logic and Design, Fifth Edition, Comprehensive 13 Using the Relational Comparison Operators (continued)

    14. Programming Logic and Design, Fifth Edition, Comprehensive 14 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) Using nested if statements Second selection structure is contained entirely within one side of first structure else clause paired with last if

    15. Programming Logic and Design, Fifth Edition, Comprehensive 15 Understanding AND Logic (continued)

    16. Programming Logic and Design, Fifth Edition, Comprehensive 16 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

    17. Programming Logic and Design, Fifth Edition, Comprehensive 17 Combining Decisions in an AND Selection Conditional AND operator allows you to 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

    18. Programming Logic and Design, Fifth Edition, Comprehensive 18 Combining Decisions in an AND Selection (continued)

    19. Programming Logic and Design, Fifth Edition, Comprehensive 19 Combining Decisions in an AND Selection (continued)

    20. Programming Logic and Design, Fifth Edition, Comprehensive 20 Avoiding Common Errors in an AND Selection Second decision must be made entirely within the first decision Range of values – every value between low and high limits In most programming languages logical AND is a binary operator Requires complete Boolean expression on both sides

    21. Programming Logic and Design, Fifth Edition, Comprehensive 21 Understanding OR Logic When you want to take action when one or the other of two conditions is true Example: Salespeople get bonus when they have achieved one of two goals: Sell at least five items Sell at least $2,000 in merchandise itemsSold >= ITEMS_MIN? If true, assign $300 bonus

    22. Programming Logic and Design, Fifth Edition, Comprehensive 22 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 Eliminates as many repetitions as possible of second decision

    23. Programming Logic and Design, Fifth Edition, Comprehensive 23 Combining Decisions in an OR Selection Conditional OR operator allows you to 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, so consider efficiency Computer can ask only one question at a time

    24. Programming Logic and Design, Fifth Edition, Comprehensive 24 Combining Decisions in an OR Selection (continued)

    25. Programming Logic and Design, Fifth Edition, Comprehensive 25 Combining Decisions in an OR Selection (continued)

    26. Programming Logic and Design, Fifth Edition, Comprehensive 26 Avoiding Common Errors in an OR Selection Second question must be self-contained structure with one entry and exit point Request for A and B in English often translates to a request for A or B logically Example: “Give a bonus to anyone who has sold at least three items and to anyone who has sold $2000” “Give a bonus to anyone who has sold at least three items or $2000”

    27. Programming Logic and Design, Fifth Edition, Comprehensive 27 Avoiding Common Errors in an OR Selection (continued)

    28. Programming Logic and Design, Fifth Edition, Comprehensive 28 Avoiding Common Errors in an OR Selection (continued)

    29. Programming Logic and Design, Fifth Edition, Comprehensive 29 Avoiding Common Errors in an OR Selection (continued)

    30. Programming Logic and Design, Fifth Edition, Comprehensive 30 Avoiding Common Errors in an OR Selection (continued)

    31. Programming Logic and Design, Fifth Edition, Comprehensive 31 Avoiding Common Errors in an OR Selection (continued)

    32. Programming Logic and Design, Fifth Edition, Comprehensive 32 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 >

    33. Programming Logic and Design, Fifth Edition, Comprehensive 33 Making Selections within Ranges (continued)

    34. Programming Logic and Design, Fifth Edition, Comprehensive 34 Making Selections within Ranges (continued)

    35. Programming Logic and Design, Fifth Edition, Comprehensive 35 Understanding Common Errors Using Range Checks Avoid dead or unreachable paths 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

    36. Programming Logic and Design, Fifth Edition, Comprehensive 36 Understanding Precedence When Combining AND and OR Selections Combine multiple AND and OR operators in an expression When multiple conditions must all be true, use multiple ANDs if score1 >= 75 AND score2 >= 75 AND score 3 >= 75 then classGrade = “Pass” else classGrade = “Fail” endif

    37. Programming Logic and Design, Fifth Edition, Comprehensive 37 Understanding Precedence When Combining AND and OR Selections (continued) When only one of multiple conditions must be true, use multiple ORs if score1 >= 75 OR score2 >= 75 OR score3 >= 75 then classGrade = “Pass” else classGrade = “Fail” endif

    38. Programming Logic and Design, Fifth Edition, Comprehensive 38 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 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”

    39. Programming Logic and Design, Fifth Edition, Comprehensive 39 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

    40. Programming Logic and Design, Fifth Edition, Comprehensive 40 Understanding Precedence When Combining AND and OR Selections (continued)

    41. Programming Logic and Design, Fifth Edition, Comprehensive 41 The Case Structure Provides a series of alternatives based on the value of a single variable Replaces a series of chained if-else statements Makes code easier to read

    42. Programming Logic and Design, Fifth Edition, Comprehensive 42 The Case Structure (continued)

    43. Programming Logic and Design, Fifth Edition, Comprehensive 43 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

    44. Programming Logic and Design, Fifth Edition, Comprehensive 44 Using Decision Tables (continued)

    45. Programming Logic and Design, Fifth Edition, Comprehensive 45 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

    46. Programming Logic and Design, Fifth Edition, Comprehensive 46 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)

    47. Programming Logic and Design, Fifth Edition, Comprehensive 47 Using Decision Tables (continued) To create a decision table (continued): Add rows to list possible outcome actions Choose one required outcome for each combination

    48. Programming Logic and Design, Fifth Edition, Comprehensive 48 Using Decision Tables (continued)

    49. Programming Logic and Design, Fifth Edition, Comprehensive 49 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

    50. Programming Logic and Design, Fifth Edition, Comprehensive 50 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

More Related