1 / 35

Problem Solving with Decisions

Problem Solving with Decisions. Chapter 6. The decision logic structure. Uses the If/Then/Else instruction . It tells the computer that If a condition is true , then execute a set of instructions, or Else execute another set of instructions. The else part is optional

bell
Download Presentation

Problem Solving with Decisions

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. Problem Solving with Decisions Chapter 6

  2. The decision logic structure • Uses the If/Then/Else instruction . • It tells the computer that If a condition is true , then execute a set of instructions, or Else execute another set of instructions. • The else part is optional • As there is not always a set of instruction if the condition are false .

  3. Common Forms of decision structure • If <condition (s)> • Then • <True instructions> • Else • <False instructions> T F • A condition can be one of four things : • Logical expression: (AND, OR NOT) • relational expression(>, <=, <, >=,…..) • Variable logic data type. • Combination of Relational , logic and mathematical operators. • Note: the condition part can combine more than one condition using the logical operators.

  4. Examples . • A < B ( a, b the same data type numeric or character ) • X + 5 > = Z ( X , Z are numeric ) • E < 5 OR F < 10 • DataOk ( is logical datum )

  5. The decision logic structure • Single condition : • A simple decision with only one condition and one action or set of actions . • Multiple condition : • Decisions in which you have multiple conditions that lead to one action or set of actions . • You will use logical operators to connect the conditions .

  6. Single condition one possible action or set of action • write an algorithm and corresponding flowchart that read a student grade on a test which is out of 10, then alerts the student if his grade is under 4.

  7. StudentAlert() Integer grade Enter grade F If grade < 4 T Print “Alert: You must study hard” End

  8. Single Condition one Possible Actions or Sets of Actions • assume you are calculating pay at an hourly rate , and overtime pay ( over 40 hours ) at 1.5 times the hourly rate .

  9. Single Condition Two Possible Actions or Sets of Actions

  10. The Multiple If/Then/Else • Three type of decision logic used to for solution consisting for more than one decision: • Straight-through Logic • Positive Logic • Negative Logic

  11. The Multiple If/Then/Else 1. Straight-through logic : • All of the decisions are processed sequentially , one after the other. 2. positive logic : • Not all of the instruction are processed • the flow of the processing continues through the module when the resultant of a decisions is true. • If the resultant is false another decision is processed until the resultant is true or there is no decision to be processed .

  12. The Multiple If/Then/Else 3. negative logic : • the flow of the processing continues through the module when the resultant of a decisions is false . • when the resultant is true , another decision is processed until the resultant is false or there is no decision to be processed .

  13. TheNested Decision • In algorithms containing multiple decisions, you may write nestedIf/Then/Else instructions. • Decisions using positive and negative logic use nested If/Then/Else instructions. • Decisions using straight-through logic do not. • Nested If/Then/Else instructions in which each level of a decision is embedded in a level before it. • Use the nested If/then/Else when one of several possible actions or set of action are to be processed .

  14. Nested If/Then/Else Instructions

  15. 1.Straight-through • All decisions (Conditions) are processed sequentially, one after another. • There is no else part of the instructions . • is required when all the decisions have to be processed , and thy are independent of each other . • The false branch always goes to the next decision. • The true branch goes to the next decision after the instruction for the true have been processed . • Least efficient, why? • Because all the decisions must be processed .

  16. 1- Straight-Through Logic Example 1 Write an algorithm to change the value of X to 0 when X becomes greater then 100, and to change the value of Y to 0 when Y becomes greater then 250.

  17. Straight-Through Logic – Example 2

  18. 1- Straight-Through Logic Example 2 Write an algorithm to find the amount to charge people of varying ages for a concert ticket. When a person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.

  19. Straight-Through Logic

  20. 2. Positive Logic • The easiest type to use. Why??!! • It is way we think • Positive logic works in the following manner: • If the condition is true  • the computer follows a set of instructions & continues processing. • If the condition is false  • the computer process another decisionuntil the resultant is true, or there are no more decisions to process.

  21. 2. Positive Logic Example 1 Write an algorithm and corresponding flowchart to find the amount to charge people of varying ages for a concert ticket. When a person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.

  22. Positive Logic – Example 1

  23. 2. Positive Logic Example 2 Design an algorithm that Calculate the commission rate of a sales person, given the amount of sales. When the salesperson has sold less than or equal to $2000, the commission is 2%. When the sales total is more than $2000, and less than or equal to $4000, the commission is 4%. When the sales total is more than $4000, and less than or equal to $6000, the commission is 7%. When the person has sold more than $6000, the commission is 10%.

  24. Positive Logic – Example 2

  25. The Conditions in example-2 Set Up in a Different Way

  26. 3. Negative Logic • Negative logic works in the following manner: • If the condition is true  • The computer process another decision until the resultant is false, or there are no more decisions to process. • If the condition is false  • The computer follows a set of instructions & continues processing. • It is often advantageous to use negative logic to decrease the number of tests or to improve the readability of a program .

  27. 3. Negative Logic Example 1 Write an algorithm and corresponding flowchart to find the amount to charge people of varying ages for a concert ticket. When a person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.

  28. Negative Logic – Example 1 age =˃ 16 age =˃ 65 age =˃ 16 age =˃ 65

  29. 2. Negative Logic Example 2 Design an algorithm that Calculate the commission rate of a sales person, given the amount of sales. When the salesperson has sold less than or equal to $2000, the commission is 2%. When the sales total is more than $2000, and less than or equal to $4000, the commission is 4%. When the sales total is more than $4000, and less than or equal to $6000, the commission is 7%. When the person has sold more than $6000, the commission is 10%.

  30. Negative Logic – Example 2

  31. The Conditions in example-2 Set Up in a Different Way

  32. Logic Conversion • If there are no instruction for the true section of decision instruction , Then it is better to convert the logic type : • Change all < to >= • Change all <= to > • Change all > to <= • Change all >= to < • Change all = to <> • Change all <> to = • Interchange all of the Then set of instructions with the corresponding Else set of instructions.

  33. Conversion from Positive Logic to Negative Logic

  34. Conversion from Positive Logic to Negative Logic

More Related