1 / 66

Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design Fourth Edition, Comprehensive. Chapter 5 Making Decisions. Objectives. Evaluate Boolean expressions to make comparisons Use the relational comparison operators Understand AND logic Understand OR logic Use selections within ranges. Objectives (continued).

tommendez
Download Presentation

Programming Logic and Design Fourth 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 DesignFourth Edition, Comprehensive Chapter 5 Making Decisions

  2. Objectives • Evaluate Boolean expressions to make comparisons • Use the relational comparison operators • Understand AND logic • Understand OR logic • Use selections within ranges Programming Logic and Design, Introductory, Fourth Edition

  3. Objectives (continued) • Understand precedence when combining AND and OR selections • Understand the case structure • Use decision tables Programming Logic and Design, Introductory, Fourth Edition

  4. 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 Programming Logic and Design, Introductory, Fourth Edition

  5. 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 Programming Logic and Design, Introductory, Fourth Edition

  6. 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: <> Programming Logic and Design, Introductory, Fourth Edition

  7. Using the Relational Comparison Operators (continued) Programming Logic and Design, Introductory, Fourth Edition

  8. 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 Programming Logic and Design, Introductory, Fourth Edition

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

  10. Evaluating Boolean Expressions to Make Comparisons (continued) • Dual-alternative (or binary) selection structure: • Also called an if-then-else structure Programming Logic and Design, Introductory, Fourth Edition

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

  12. Evaluating Boolean Expressions to Make Comparisons (continued) • Single-alternative (or unary) selection structure • Also called an if-then structure Programming Logic and Design, Introductory, Fourth Edition

  13. Using the Relational Comparison Operators (continued) Each calculates a discount of 10% only when the customer age is 65 years old or greater IF customerAge >=65 THEN discount = 0.10ELSE discount = 0ENDIF IF customerAge < 65 THEN discount = 0ELSE discount = 0.10ENDIF IF customerAge < 64 THEN discount = 0ELSE discount = 0.10ENDIF ALL ARE CORRECT – THERE ARE NO WRONG WAYS! Programming Logic and Design, Introductory, Fourth Edition

  14. 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 Programming Logic and Design, Introductory, Fourth Edition

  15. Using the Relational Comparison Operators (continued) • Rephrase in the positive • Some comparisons are clearer when negative is used Programming Logic and Design, Introductory, Fourth Edition

  16. Understanding AND Logic • AND decision • Requires that both (ALL) of two tests evaluate to True • Requires a nested decision (nested if) Programming Logic and Design, Introductory, Fourth Edition

  17. Understanding AND Logic (continued) • Developing the application • The input data Programming Logic and Design, Introductory, Fourth Edition

  18. Understanding AND Logic (continued) • Developing the application • The intended output: Programming Logic and Design, Introductory, Fourth Edition

  19. Understanding AND Logic (continued) • Developing the application • The mainline logic: Programming Logic and Design, Introductory, Fourth Edition

  20. Understanding AND Logic (continued) • housekeeping() module: Programming Logic and Design, Introductory, Fourth Edition

  21. Understanding AND Logic (continued) • createReport() module: Programming Logic and Design, Introductory, Fourth Edition

  22. Understanding AND Logic (continued) Programming Logic and Design, Introductory, Fourth Edition

  23. 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 Programming Logic and Design, Introductory, Fourth Edition

  24. 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 YES path NO path and completion of YES path Programming Logic and Design, Introductory, Fourth Edition

  25. 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 Programming Logic and Design, Introductory, Fourth Edition

  26. 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 Programming Logic and Design, Introductory, Fourth Edition

  27. Combining Decisions in an AND Selection (continued) Single IF statement Programming Logic and Design, Introductory, Fourth Edition

  28. Combining Decisions in an AND Selection (continued) Multiple IF statement Programming Logic and Design, Introductory, Fourth Edition

  29. Avoiding Common Errors in an AND Selection • Failure to nest 2nd decision entirely within 1st decision Tests each decision separately no matter the outcome of the other decision. Programming Logic and Design, Introductory, Fourth Edition

  30. 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 Programming Logic and Design, Introductory, Fourth Edition

  31. Understanding OR Logic (continued) • createReport() module: IF empMedicalIns = Y OR empDentalIns = “Y” THEN print empIdNumber, empLastName, empFirstNameENDIF Programming Logic and Design, Introductory, Fourth Edition

  32. Avoiding Common Errors in an OR Selection (continued) • Incorrect interpretation of English: • Casual use of AND when logic requires OR Programming Logic and Design, Introductory, Fourth Edition

  33. Avoiding Common Errors in an OR Selection (continued) • Correct logic: Programming Logic and Design, Introductory, Fourth Edition

  34. Avoiding Common Errors in an OR Selection (continued) • Incorrect interpretation of English • Use of OR when AND logic is required > 12: All patrons (including 65 and above) < 65: All patrons (including 12 and below) Programming Logic and Design, Introductory, Fourth Edition

  35. Avoiding Common Errors in an OR Selection (continued) • Correct logic: Programming Logic and Design, Introductory, Fourth Edition

  36. Writing OR Decisions for Efficiency • How many decisions? Programming Logic and Design, Introductory, Fourth Edition

  37. Writing OR Decisions for Efficiency (continued) Programming Logic and Design, Introductory, Fourth Edition

  38. 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) Programming Logic and Design, Introductory, Fourth Edition

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

  40. Combining Decisions in an OR Selection (continued) • Using an OR operator: Programming Logic and Design, Introductory, Fourth Edition

  41. Combining Decisions in an OR Selection (continued) • What the computer actually does: Programming Logic and Design, Introductory, Fourth Edition

  42. Using Selections Within Ranges • Rangecheck: 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 > Programming Logic and Design, Introductory, Fourth Edition

  43. Range Selection Task: print if empRate is 10 or 11 Range: 10 -11 to get TRUE Programming Logic and Design, Introductory, Fourth Edition

  44. SYNTAX error Range Selection Good Programming Logic and Design, Introductory, Fourth Edition

  45. Using Selections Within Ranges (continued) • Using high-end values in the range check: Programming Logic and Design, Introductory, Fourth Edition

  46. Using Selections Within Ranges (continued) • Using low-end values in the range check: Programming Logic and Design, Introductory, Fourth Edition

  47. Common Errors Using Range Checks (continued) Unnecessary Unnecessary Programming Logic and Design, Introductory, Fourth Edition

  48. 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 Programming Logic and Design, Introductory, Fourth Edition

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

  50. Understanding Precedence When Combining AND and OR Selections (continued) AND OR Programming Logic and Design, Introductory, Fourth Edition

More Related