370 likes | 911 Views
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
E N D
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 • 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.
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.
StudentAlert() Integer grade Enter grade F 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 .
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
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 .
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 .
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.
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.
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.
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.
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%.
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 .
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.
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%.
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.