0% found this document useful (0 votes)
2 views

CH5-Problem Solving With Sequential Logic and Decisions

Chapter 5 discusses problem-solving using sequential logic and decision-making structures in programming. It covers algorithm instructions, flowchart symbols, and various logic structures including sequential, decision, and loop structures. The chapter also explains different types of decision logic such as straight-through, positive, and negative logic, along with examples and guidelines for their implementation.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CH5-Problem Solving With Sequential Logic and Decisions

Chapter 5 discusses problem-solving using sequential logic and decision-making structures in programming. It covers algorithm instructions, flowchart symbols, and various logic structures including sequential, decision, and loop structures. The chapter also explains different types of decision logic such as straight-through, positive, and negative logic, along with examples and guidelines for their implementation.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 82

Chapter 5: Problem

Solving with Sequential


logic and Decisions
SW 100: Principles of Programming and Problem Solving

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.

• The instruction includes the name of the module


and all parameters. For example
Print (Hours, Pay)
• The name of the module also may be written at the
beginning of the algorithm, which is the case
throughout this text.

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

• Variable=(commonly called the assignment instruction)


enables a user to instruct the computer to store a
constant, a variable, or an expression in a named
variable memory location. For example,
Pay = Hours * Wage
Dr.Hanin 6
Abdulrahman
Algorithm
•Instructions
The Write instruction outputs values to the printer.
The list of information to be printed follows the
Write, as in
Write Name, Age
• The Print instruction is similar to the Write instruction
except that the output is directed to the screen
rather than the printer

• The Process instruction tells the computer to


process the instructions of a named module
before continuing.
• For example Dr.Hanin
Abdulrahman
7
Algorithm
Instructions
• The End, Exit, or Return instruction specifies the
completion of a module.

• End is used to end the Control module and indicates


that the processing of the solution is complete.

• Exit is used to end a subordinate module if there is no


return value, and indicates that the processing will
continue in another module, the module where the
Process instruction originated.

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

2. The decision structure branches to execute one


of two possible sets of instructions.

3. The loop structure executes a set of


instructions many times.
Dr.Hanin 10
Abdulrahman
The Sequential Logic
•Structure
All problems use the sequential structure, and most
problems use it in conjunction with one or more of
the other logic structures.

• A programmer who uses the sequential logic structure


is asking the computer to process a set of instructions
in sequence from the top to the bottom of an algorithm

Dr.Hanin 11
Abdulrahman
The Sequential Logic
Structure
In general, the form of the algorithm looks
like this:

ModuleName (list of parameters)


1. Instruction
2. Instruction
3. ….
.
.
XX.End, Exit, or Return (variable)
Dr.Hanin 12
Abdulrahman
The Decision Logic
Structure
• 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 instructions if the conditions are false.
Dr.Hanin 13
Abdulrahman
The Decision Logic
Structure
• Notice the brackets and the
indentation in the illustration
of the algorithm of the
decision instruction.

• Programmers use these


features when they are writing
instructions in the decision
structure because they
improve readability.
Dr.Hanin 14
Abdulrahman
The Decision Logic
Structure
A condition can be one of four things:
1. A logical expression, that is, an expression that uses
logical operators (AND, OR, and NOT)
2. An expression using relational operators (greater
than, less than, greater than or equal to, less than or
equal to, equal to, and not equal to)
3. A variable of the logical data type (True or False)
4. A combination of logical, relational, and
mathematical operators.

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 )

note: the data type of DriverLicense is logical.

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.

•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.
Dr.Hanin 18
Abdulrahman
Flowchart Diagram of the Decision
Structure

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

A simple decision with only one condition and one


action or set of actions (one instruction or set of
instructions) for True and another for False is
relatively simple.

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:

Hourly rate: Pay=PayRate * Hours


Overtime pay: Pay=PayRate * (40 + 1.5 * (Hours-40))
• If the hours are greater than 40, Then the pay is
calculated for overtime, or Else the pay is calculated
in the usual 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.

•In these decisions you will use logical


operators to connect the 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.

•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.

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.

• However, 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.

• It is often used in conjunction with the other two logic


types and in data validation (checking data to make
sure it is correct).
Dr.Hanin 30
Abdulrahman
Straight-through logic:
Example
Example 1:
1
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. The conditions are the following:
Age Charge
Age < 16 7
Age >= 16 and Age < 65 10
Age >= 65 5
Dr.Hanin 31
Abdulrahman
Straight-Through Logic:
Example 1

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.

•Based on these criteria, the only appropriate


logic type for the decision in Example 2 would
be straight-through logic.

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.

•When the age is NOT less than 16, it has to be


greater than or equal to 16.

•Therefore, the third decision in the set of


conditions is not necessary.
Dr.Hanin 40
Abdulrahman
Using Positive Logic:
Example
Example1: 2
Calculate the commission rate for a
salesperson, given the amount of sales.
• When the salesperson has sold less than or equal to
$2,000 worth of goods, the commission is 2%.
• When the sales total is more than $2,000 and less
than or equal to $4,000, the commission is 4%.
• When the sales total is more than $4,000 and less
than or equal to $6,000, the commission is 7%.
• When the person has sold more than
$6,000, the commission is 10%.

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.

•Whenever the resultant is True, another decision is


processed until the resultant is False, or there are
no more decisions to process.

•At that time, the True branch processes the


remaining instructions. Negative logic is the
hardest to use and understand.
Dr.Hanin 46
Abdulrahman
Negative Logic:
Example
Example 1:
1
The same problem, but using negative logic
‫بداية نعمل على عكس الشروط للتحويل‬

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.

•Decisions using positive and negative logic use


nested If/Then/Else instructions.

•Decisions using straight-through logic do not


using Nested if/Then/Else
Dr.Hanin 53
Abdulrahman
Revisio
n
So far, we have covered the following:
• If/Then/Else instruction
• Single and Multiple
Conditions
• Multiple If/Then/Else
Instructions
• Straight-through Logic
• Positive logic
• Negative logic

Now let’s move to the logic


Dr.Hanin 54
Abdulrahman
Logic
Conversion
• There are times when it is necessary to change the
logic from positive to negative or vice versa in order
to improve the efficiency or readability of a solution.

• In a decision, there must always be instructions for


a True section, but not always for a False section.

• If there are no instructions for the True section of a


decision instruction, then it is better to convert the
logic type.

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.

• If you find at the end of the analysis that more than


one of the logic types could be equally effective, then
simply choose one.
Dr.Hanin 59
Abdulrahman
Which Decision Logic?
Example
Example 1:
1
The problem is to calculate a pay bonus given
the set of conditions:

From the data given, which logic type is the most


efficient?

Dr.Hanin 60
Abdulrahman
which logic type is the most efficient?

Four Ways to Design a Set


of Conditions
1.Ascending Positive Logic
2.Descending Positive Logic
3.Ascending Negative Logic
4.Descending Negative Logic
Which Decision Logic?
Example 1

1. Ascending Positive Logic

Dr.Hanin 62
Abdulrahman
Which Decision Logic?
Example 1

2. Descending Positive Logic

Dr.Hanin 63
Abdulrahman
Which Decision Logic?
Example 1

3. Ascending Negative Logic

Dr.Hanin 64
Abdulrahman
Which Decision Logic?
Example 1

4. Descending Negative Logic

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.

•In such a case discovering all of the


actions that correspond to particular
conditions can be complicated.

•A good way to simplify this process is to


draw a decision table. You would draw a
decision table during problem analysis.
Dr.Hanin 67
Abdulrahman
Decision
Tables
A decision table consists of four 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

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

You might also like