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 As there is not always a set of instruction if the condition are false . Common Forms of decision structure T F •If <condition (s)> •Then •<True instructions> •Else •<False instructions> 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. 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 ) 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 . 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. Algorithm Flowchart StudentAlert() 1.Integer grade 2.Enter grade 3.If grade < 4 1. Print “Alert: You must study hard” 4.end StudentAlert() Integer grade Enter F grade If grade < 4 T Print “Alert: You must study hard” End 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 . Single Condition Two Possible Actions or Sets of Actions The Multiple If/Then/Else Three type of decision logic used to for solution consisting for more than one decision: Straight-through Positive Logic Negative Logic Logic 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 . 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 . The Nested Decision In algorithms containing multiple decisions, you may write nested If/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 . Nested If/Then/Else Instructions 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 . 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. X > 100 X=0 Y > 250 Y=0 Straight-Through Logic – Example 2 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. Age Charge Age < 16 7 Age >= 16 AND Age < 65 10 Age >= 65 5 Straight-Through Logic 2. Positive Logic The easiest type to use. Why??!! It is way we think Positive logic works in the following manner: If If the condition is true the computer follows a set of instructions & continues processing. the condition is false the computer process another decision until the resultant is true, or there are no more decisions to process. 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. Age Charge Age < 16 7 Age >= 16 AND Age < 65 10 Age >= 65 5 Positive Logic – Example 1 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%. SALES Commission ≤ 2000 .02 2001 - 4000 .04 4001 – 6000 .07 > 6000 .10 Positive Logic – Example 2 The Conditions in example-2 Set Up in a Different Way 3. Negative Logic Negative logic works in the following manner: If If the condition is true The computer process another decision until the resultant is false, or there are no more decisions to process. 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 . 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. Age Charge Age < 16 7 Age >= 16 AND Age < 65 10 Age >= 65 5 Negative Logic – Example 1 age ˃= 16 age ˃= 65 age ˃= 16 age ˃= 65 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%. SALES Commission ≤ 2000 .02 2001 - 4000 .04 4001 – 6000 .07 > 6000 .10 Negative Logic – Example 2 The Conditions in example-2 Set Up in a Different Way 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. Conversion from Positive Logic to Negative Logic Conversion from Positive Logic to Negative Logic