Professional Documents
Culture Documents
Problem Solving With Decisions: T.Fatin Alhila
Problem Solving With Decisions: T.Fatin Alhila
Part 2
1 T.Fatin Alhila
Decision Logic Structure
2 T.Fatin Alhila
Common Forms of decision structure
•If <condition(s)>
•Then
T •<True instructions>
•Else
F
•<False instructions>
3 T.Fatin Alhila
Decision Logic Structure
4 T.Fatin Alhila
Decision Logic Structure
- Logical operators are used to link more than one condition together.
- The programmer can set up the condition for a decision through the use
of operands and various operators.
6 T.Fatin Alhila
Decision Logic Structure
- These conditions can be used alone or linked with other conditions for
use in the instruction.
- The programmer derives the information needed to set up the
condition(s) for a decision from the problem itself during problem
analysis.
- The True branch and the False can come from any of the lower three
points of the diamond.
- The convention is to draw the True branch on the right and the False
branch on the left or bottom, although it depends on the decisions to be
made.
7 T.Fatin Alhila
Common Forms
The If Statement
The Single Selection If/Then
The Double If/Then/Else
The Multiple If/Then/Else
Straight-through Logic
8 T.Fatin Alhila
1- Single Selection: If/Then
one condition one instruction(true section) no else part
9 T.Fatin Alhila
Algorithm Flowchart
StudentAlert()
1. Integer grade StudentAlert()
2. Enter grade
3. If grade < 4 Integer grade
then
1. Print “Alert:You must study hard”
1. end Enter grade
F T
If grade < 4
Print “Alert:You
must study hard”
End
10 T.Fatin Alhila
2- Double If/Then /Else Selection
one condition two instruction –true-false-else part
overtime pay
Pay=PayRate * (40 + 1.5 * (Hours-40))
11 T.Fatin Alhila
Algorithm Flowchart
PayCalculate()
1. Integer Hours, PayRate PayCalculate()
2. Enter Hours, PayRate
3. Real Pay
4. If Hours > 40 Integer Hours, PayRate
1. Then
1. Pay=PayRate * (40 + 1.5 Enter Hours, PayRate
* (Hours-40))
2. Else Real Pay
1. Pay=PayRate * Hours
5. End
F If Hours > T
40
Pay=PayRate * (40
Pay=PayRate * + 1.5 *
Hours (Hours-40))
12
End T.Fatin Alhila
3- Multiple Double If/Then /Else Selection
- Decision in which you have multiple conditions that lead to one action
or set of actions for True and False are slightly more complicated than
those with single condition.
13 T.Fatin Alhila
Using Straight-through logic
There is no Else part of the instructions; 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.
14 T.Fatin Alhila
Using Straight-through logic
- You must use it to solve certain problems, those that require two or
more unrelated decisions, and those in which all decisions must be
processed.
15 T.Fatin Alhila
Straight-Through Logic Example
Write an algorithm to change the value of X to 0 when X becomes greater
than 100, and to change the value of Y to 0 when Y becomes greater than
250.
16 T.Fatin Alhila
Straight-Through Logic Example
Algorithm Flowchart
ChangeValue()
ChangeValue()
1. Integer X,Y
2. Enter X,Y Integer X,Y
3. If X > 100
1. Then
1. X= 0 Enter X,Y
4. If Y > 250
1. Then T
If X > 100
1. Y= 0
5. End X =0
F
T
If Y > 250
F Y =0
18 T.Fatin Alhila
Logic Conversion
Rules to convert from positive logic to negative logic or vice
versa:
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.
19 T.Fatin Alhila
- For example:
It is to find the amount to charge people of varying ages for a concert
ticket. When the person is under 16, the charge is $7; when the person
is 65 or over, the charge is $5; all others are charged $10.
4. End
End
21 T.Fatin Alhila
Figure 6.12 Conversion from Positive Logic
to Negative Logic
22 T.Fatin Alhila
Figure 6.12 Conversion from Positive Logic
to Negative Logic
23 T.Fatin Alhila
Decision Table
A good way to simplify the process of discovering
complicated actions and conditions is to draw a decision
table.
Most consist of 4 parts:
1. The conditions
2. The actions
3. The combinations of True and False for the conditions
4. The action to be taken or the consequences for each
combination of conditions.
24 T.Fatin Alhila
Table 6.1 Decision Table Format
• The total number of possible combinations of True or False for the conditions is
2^ #conditions
25 T.Fatin Alhila
Decision table Example
Set up a decision table for a store policy for charging a purchase.
There are three conditions:
1. The purchase is less than $100.
2. The last payment to the account was made in the last 30 days.
3. The balance of the account is less than $1000.
The following actions could be taken:
1. Credit is okay, and the customer can charge the item.
2. Refer the customer to the credit department.
3. Credit is denied and the customer cannot charge the item.
26 T.Fatin Alhila
Table 6.2 Decision Table
27 T.Fatin Alhila
Decision Table
The four steps to develop a flowchart from the decision table
are:
1. Draw all decisions in flowchart form.
2. Compare the true and false sides of each decisions, starting
with the first one.
3. Eliminate any decisions that have the same instructions on
both the true and false sides, keeping the true consequence
or action.
4. Redraw the flowchart.
28 T.Fatin Alhila
Development of flowchart from the
Decision table.
29 T.Fatin Alhila
Elimination of Conditions
30 T.Fatin Alhila
Final Flowchart
31 T.Fatin Alhila
Decision Table Example#2
Set up a Decision Table for Numeric Grades associated with
Letter Grades?
A 90-100
B 80-89
C 70-79
D 60-69
F Below 60
32 T.Fatin Alhila
Decision Table Solution
Letter Grade
(Solution-Action)
A B C D F
Numeric Grade
(Condition)
90 <= n X
80 <= n < 90 X
70 <= n < 80 X
60 <= n < 70 X
< 60 X
33 T.Fatin Alhila
let’s work a problem
34 T.Fatin Alhila
Putting It All Together
The Putting It All Together (PIAT) are designed to show that how to
pull together and use the concepts from a previous sections.
The PIAT demonstrates how to use the six steps of problem solving on
the computer to develop a solution that uses the sequential and the
decision logic structures.
35 T.Fatin Alhila
Putting It All Together
- For example:
The Floral Company sells to wholesale and retail buyers. The wholesale
buyer needs a resale number in order to buy at no tax and to receive
discounts. The retail buyer pays 6% tax.
36 T.Fatin Alhila
A Fantastic Floral Company_
PAC (Problem Analysis Chart)
37 T.Fatin Alhila
Fantastic Floral Company –
Interactivity (Structure) Chart
38 T.Fatin Alhila
Fantastic Floral Company – IPO Chart
39 T.Fatin Alhila
Coupling Diagram and Data Dictionary
...
40 T.Fatin Alhila
The Algorithms and Flowcharts –
Control Module
41 T.Fatin Alhila
The Algorithms and Flowcharts –
Read Module
42 T.Fatin Alhila
The Algorithms and Flowcharts –
Calc Module
43 T.Fatin Alhila
The Algorithms and Flowcharts
Print Module
44 T.Fatin Alhila