CH5-Problem Solving With Sequential Logic and Decisions
CH5-Problem Solving With Sequential Logic and Decisions
Dr.Hanin 1
Abdulrahman
Overvie
w
• Algorithm Instructions and Flowchart
Symbols
• The Sequential Logic Structure
• The Decision Logic Structure
Dr.Hanin 2
Abdulrahman
Algorithm Instructions and Flowchart
Symbols
Dr.Hanin 3
Abdulrahman
Algorithm Instructions and Flowchart
Symbols
Dr.Hanin 4
Abdulrahman
Algorithm
•Instructions
The Start instruction is always understood, if not stated,
at the beginning of an algorithm and flowchart and
indicates where the module begins.
Dr.Hanin 5
Abdulrahman
Algorithm
•Instructions
The Enter instruction enables the user to enter
data into memory locations with variable
names.
• The list of variable names follows the Enter
instruction. For example,
Enter Name, Age
Dr.Hanin 8
Abdulrahman
Algorithm
Instructions
• The Return (variable) is used to place a value in the
name of the module. The Return is used when the
module is to be processed within an expression.
Dr.Hanin 9
Abdulrahman
The logic
structures
1. The sequential structure executes
instructions one after another in a
sequence
Dr.Hanin 11
Abdulrahman
The Sequential Logic
Structure
In general, the form of the algorithm looks
like this:
Dr.Hanin 15
Abdulrahman
The Decision Logic
Structure
Some examples of conditional expressions
are as follows:
1. A < B
2. X + 5 >= Z
3. X > 0 OR Y < 100
4. DataOk (DataOk is a logical datum)
Dr.Hanin 16
Abdulrahman
The Decision Logic
•Structure
Logical operators are used to link more than one
condition together.
• For example, consider a store policy requiring that to
cash a check, a customer must have a driver’s license,
AND the check must be under $50. Two conditions are
linked by the logical operator AND.
( Check < 50 AND DriverLicense )
Dr.Hanin 17
Abdulrahman
The Decision Logic
•Structure
The programmer can set up the conditions
for a decision through the use of operands
and various operators.
Dr.Hanin 19
Abdulrahman
Single Condition—Two Possible Actions
or Sets of Actions
Dr.Hanin 20
Abdulrahman
Next
…
• Common forms of decision logic
structure
• Logic conversion
• Decision table
Dr.Hanin 21
Abdulrahman
Common Forms of the Decision Logic
Structure
• If/Then/Else instruction
• Single Condition
• Multiple Condition
• Multiple If/Then/Else
Instructions
•Straight-through Logic
•Positive logic
•Negative logic
Dr.Hanin 22
Abdulrahman
Single
Condition
Dr.Hanin 23
Abdulrahman
Single
Condition
• For example, assume you are calculating pay at an
hourly rate and overtime pay (over 40 hours) at 1.5
times the hourly rate. The decision to calculate pay
would be stated in the following way:
Dr.Hanin 24
Abdulrahman
Single
Condition
Dr.Hanin 25
Abdulrahman
Multiple
•Conditions
Decisions in which you have multiple conditions
that lead to one action or set of actions for True
and another action or set of actions for False
are slightly more complicated than those with
single conditions.
Dr.Hanin 27
Abdulrahman
Multiple If/Then/Else
Instructions
There are three types of decision logic you will
use to write algorithms for solutions consisting of
more than one decision :
1. Straight-through Logic
2. Positive logic
3. Negative logic
Dr.Hanin 28
Abdulrahman
Straight-through
logic
•Straight-through logic means that all of the
decisions are processed sequentially, one after
the other.
Dr.Hanin 29
Abdulrahman
Straight-through
•logic
Straight-through logic is the least efficient of all
types of decision logic because all the decisions
must be processed.
Dr.Hanin 32
Abdulrahman
Straight-through logic:
Example
Example 2:
2
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.
Each decision is independent of the other.
The conditions are
X > 100 X becomes 0
Y > 250 Y becomes 0
Dr.Hanin 33
Abdulrahman
Straight-through logic:
•Example 2
Straight-through logic is required when all the
decisions have to be processed, and when they
are independent of each other.
Dr.Hanin 34
Abdulrahman
Straight-through logic:
Example 2
Dr.Hanin 35
Abdulrahman
Using Positive
•Logic
On the other hand, with positive logic not all of the
instructions are processed.
• 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.
• Whenever the resultant is False (the Else part of the
decision), another decision in the sequence is
processed until the resultant is True, or there are no
more decisions to process.
• At that time, the False branch processes the
remaining instructions.
Dr.Hanin 36
Abdulrahman
Positive Logic:
Example
Example 1:
1
The same problem, but using positive logic
Age Charge
Age < 16 7
Age >= 16 and Age < 65 10
Age >= 65 5
Dr.Hanin 37
Abdulrahman
Positive Logic:
Example 1
Dr.Hanin 38
Abdulrahman
Straight trough logic Vs. Positive logic on
Example 1
Positive Logic:
•Example 1
Notice that there are fewer decisions processed
than in slide 32—two compared to three—even
if all the decisions are processed.
Dr.Hanin 41
Abdulrahman
Using Positive Logic:
Example 2
The conditions are the
following:
Sales Commission
< = 2000 0.02
2001 - 4000 0.04
4001-6000 0.07
> 6000 0.10
Dr.Hanin 42
Abdulrahman
Using Positive Logic: Ascending Positive Logic
Example 2
Dr.Hanin 43
Abdulrahman
Using Positive Logic:
Example 2
Dr.Hanin 44
Abdulrahman
Using Positive Logic Set Up in a Different
Way: Example 2 Descending Positive Logic
Negative
logic
•Negative logic is similar to positive logic except that
the flow of the processing continues through the
module when the resultant of a decision is False.
Age Charge
Age < 16 7
Age >= 16 and Age < 65 10
Age >= 65 5
Dr.Hanin 47
Abdulrahman
Negative Logic: هذا المثال فيه خطأ
Example 1
Dr.Hanin 48
Abdulrahman
Negative Logic:
Example
Example 2:
2
The same problem but using negative logic:
بداية نعمل على عكس الشروط للتحويل
Sales Commission
< = 2000 0.02
2001 - 4000 0.04
4001-6000 0.07
> 6000 010
Dr.Hanin 49
Abdulrahman
Negative Logic: Ascending Negative Logic
Example 2
Dr.Hanin 50
Abdulrahman
Negative Logic:
Example 2
Dr.Hanin 51
Abdulrahman
Different Way Negative Logic: Example
2 Descending Negative Logic
Nested If/Then/Else
•instructions
In algorithms containing multiple decisions,
you may have to write nested If/Then/Else
instructions.
Dr.Hanin 55
Abdulrahman
Logic
Conversion
To convert from positive logic to negative logic or vice
versa, do the following:
1. Change all < to >=
2. Change all <= to >
3. Change all > to <=
4. Change all >= to <
5. Change all = to <>
6. Change all <> to =
7. Interchange all of the Then set of instructions
with the corresponding Else set of
instructions.
Dr.Hanin 56
Abdulrahman
Conversion from Positive Logic to
Negative Logic
Dr.Hanin 57
Abdulrahman
Conversion from Positive Logic to
Negative Logic
Dr.Hanin 58
Abdulrahman
Which Decision
Logic?
•The general rule is to evaluate all three
types for each solution and choose the
one that is most
readable,
requires the fewest tests,
easiest to maintain.
Dr.Hanin 60
Abdulrahman
which logic type is the most efficient?
Dr.Hanin 62
Abdulrahman
Which Decision Logic?
Example 1
Dr.Hanin 63
Abdulrahman
Which Decision Logic?
Example 1
Dr.Hanin 64
Abdulrahman
Which Decision Logic?
Example 1
Dr.Hanin 65
Abdulrahman
Four Ways to Design a Set of Conditions
Decision
•Tables
You may have a problem with multiple
conditions and multiple consequent actions.
Dr.Hanin 68
Abdulrahman
Decision Table
Format
Dr.Hanin 69
Abdulrahman
Decision Table
Format
1. Draw a decision table in the form of a rectangle
divided into the four major sections.
2. Each section is divided into smaller sections, as
needed, according to the number of possible
combinations of True and False
3. The total number of possible combinations of True
and False for the conditions is two to the power of
the number of conditions.
two conditions 2^2 = 4 possible combinations of True and
False.
three conditions 2^3 = 8 possible combinations of True
and False.
Dr.Hanin 70
Abdulrahman
Flowchart development from the
decision table
The four steps to develop a flowchart from the decision
table are the following:
1. Draw all decisions in flowchart form.
2. Compare the true and false sides of each decision,
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
Dr.Hanin 71
Abdulrahman
Starting Flowchart from the decision
table
Dr.Hanin 72
Abdulrahman
Elimination of
Conditions
• Notice that the true side of decision one is
exactly the same as the false side.
• Therefore, this decision is not needed.
• The decision and the false side can be
eliminated.
• The true side and the false side of the second
decision are different and therefore, they need
to stay.
• The same is true for the third decision.
Dr.Hanin 73
Abdulrahman
Elimination of
Conditions
Dr.Hanin 74
Abdulrahman
Final
Flowchart
Dr.Hanin 75
Abdulrahman
Decision Table
Example
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 $1,000.
Each of these three conditions could be True or False
depending on the customer. The following actions could
be taken:
4. Credit is okay, and the customer can charge the
item.
5. Refer the customer to the credit department.
6. Credit is denied andDr.Hanin
the customer cannot charge 76
Abdulrahman
Decision Table
Example
These conditions and actions are supplied by the
manager of the credit department. The manager also
states when each of the actions will take place.
Dr.Hanin 77
Abdulrahman
Starting Flowchart from
theTable
Dr.Hanin 78
Abdulrahman
Elimination of
Conditions
Dr.Hanin 79
Abdulrahman
Elimination of
Conditions
Dr.Hanin 80
Abdulrahman
Final
Algorithm
Dr.Hanin 81
Abdulrahman
Final
Flowchart
Dr.Hanin 82
Abdulrahman