Professional Documents
Culture Documents
Chapter 6 Problem Solving With Decisions
Chapter 6 Problem Solving With Decisions
Chapter 6 Problem Solving With Decisions
The Decision
Logic Structure
The Decision
Logic Structure
Multiple IF/THEN/ELSE
There are three types of decision logic you will use to
write algorithms for solutions consisting of more than
one decision. These types of decision logic include:
· Straight-through logic
· Positive logic
· Negative Logic
Problem Solving with Decisions *Property of STI
Page 2 of 40
Data Structures and Algorithms
The Decision
Logic Structure
The Decision
Logic Structure
Straight-through logic
@ Means that all of the decisions are processed
sequentially, one after the other.
@ There is no ELSE part of the instruction; the FALSE
branch always goes to the next decision, and the
TRUE branch goes to the next decision after the
instructions for the TRUE branch have been
processed.
The Decision
Logic Structure
Positive Logic
@ Allows the flow of the processing to continue
through the module instead of processing
succeeding decisions, once the resultant of a
decision is true.
The Decision
Logic Structure
Negative Logic
@ similar to positive logic except that the flow of the
processing continues through the module when the
resultant of a decision is FALSE
The Decision
Logic Structure
AGE CHARGE
AGE < 16 7
AGE >=16 and AGE < 65 10
AGE >=65 5
Problem Solving with Decisions *Property of STI
Page 7 of 40
Data Structures and Algorithms
The Decision
Logic Structure
Solution :
Algorithm Flowchart
A
IF AGE < 16
THEN
T CHARGE = 7
IF T
AGE < 16
IF AGE >= 16 AND AGE <65
THEN
T CHARGE = 10 Charge = 7
IF AGE >= 65 F
THEN
T CHARGE = 5 IF
AGE >= 16 T
and
AGE < 65
Charge = 10
IF T
AGE >= 65
Charge = 10
The Decision
Logic Structure
Algorithm Flowchart
A
IF X < 100
THEN IF T
T X = 10 X > 100
THEN
T F
Y=0
IF T
Y > 250
Y=0
The Decision
Logic Structure
ELSE
IF AGE < 65
THEN
CHARGE = 10
ELSE
CHARGE = 5
The Decision
Logic Structure
Flowchart
F IF T
AGE < 16
F IF T
AGE < 65 CHARGE = 7
CHARGE = 5 CHARGE = 10
The Decision
Logic Structure
SALES COMMISSION
<=2000 .02
2001 – 4000 .04
4001 – 6000 .07
> 6000 .10
The Decision
Logic Structure
Solution 1: Algorithm
IF SALES <= 2000
THEN
COMMISSION = .02
T ELSE
IF SALES <= 4000
THEN
COMMISSION = .04
T
ELSE
IF SALES <= 6000
THEN
F T
COMMISSION = .07
F ELSE
F COMMISSION = .1
The Decision
Logic Structure
Solution 1: Flowchart
F T
IF
SALES <=2000
F T COMMISSION =
IF .02
SALES <=4000
COMMISSION
F IF T = .04
SALES <=6000
COMMISSION COMMISSION
= .1 = .07
The Decision
Logic Structure
Solution 1: Test
The Decision
Logic Structure
Solution 2: Algorithm
IF SALES > 6000
THEN
COMMISSION = .1
T ELSE
IF SALES > 4000
THEN
COMMISSION = .07
T
ELSE
IF SALES > 2000
THEN
F T
COMMISSION = .04
F ELSE
F COMMISSION = .02
The Decision
Logic Structure
Solution 2: Flowchart
A
F T
IF
SALES > 6000
F T COMMISSION =
IF .1
SALES > 4000
COMMISSION
F IF T = .07
SALES > 2000
COMMISSION COMMISSION
= .02 = .04
The Decision
Logic Structure
Solution 2: Test
The Decision
Logic Structure
The Decision
Logic Structure
IF AGE >= 16
THEN
IF AGE >= 65
THEN
CHARGE = 5
ELSE
CHARGE = 10
ELSE
CHARGE = 7
The Decision
Logic Structure
F T
IF
AGE >= 16
F T
CHARGE = 7 IF
AGE >= 16
CHARGE = 10 CHARGE = 5
The Decision
Logic Structure
ELSE
COMMISSION = .04
ELSE
COMMISSION = .02
The Decision
Logic Structure
IF
SALES > 2000
COMMISSION =
.02 IF
SALES > 4000
COMMISSION =
.04 IF
SALES > 6000
COMMISSION = COMMISSION =
.07 .01
The Decision
Logic Structure
The Decision
Logic Structure
The Decision
Logic Structure
F T
IF
SALES <= 6000
CHARGE = .1 F T
IF
SALES <= 4000
COMMISSION =
.07
F IF T
SALES <= 2000
COMMISSION = COMMISSION =
.04 .02
The Decision
Logic Structure
The Decision
Logic Structure
Logic Conversion
The Decision
Logic Structure
The Decision
Logic Structure
The Decision
Logic Structure
The Decision
Logic Structure
Decision Tables
A decision table consists of four parts:
@ The conditions.
@ The actions.
@ The combinations of TRUE and FALSE for the conditions.
@ The action to be taken or the consequences for each
combination of conditions.
The Decision
Logic Structure
The Decision
Logic Structure
The Decision
Logic Structure
Elimination of Conditions
The Decision
Logic Structure
Final Flowchart
The Decision
Logic Structure
Decision Table
The Decision
Logic Structure
Starting Flowchart
The Decision
Logic Structure
Elimination of Condition
The Decision
Logic Structure
Final Flowchart