Problem Solving With Logic Structures
Problem Solving With Logic Structures
Structures
UNIT – 2
Introduction to Programming Structure
Objectives:
List and describe four logic structures: sequential, decision,
loops and case.
Pointers for Structuring a Program
Use modules
Use four logic structures:
Sequential structure
Decision structure
Loop structure
Case structure
the module.
Decision structure
Loop structure
Case structure
Problem Solving with Sequential Logic Structure
Objectives:
problems.
Flowchart
Algorithm
Name Age
3. End
Print
Name, Age
End
Solution Development
Objectives:
Develop problem using the decision logic structure in
conjunction with the sequential logic structure.
Use the seven problem solving tools when developing a
solution using the decision logic structure.
Use nested decision instructions to develop a problem
solution.
Distinguish the different uses of straight through positive and
negative nested decision logic structures.
Convert a positive decision logic structure to a negative
decision logic structure.
Develop decision tables given a set of policies.
If
<Condition(s)>
B
Simple Decision Statement
Algorithm Flowchart
A
B
Multiple If/Then/Else Instructions
Positive logic
Allows the flow of processing to continue through the module
instead of succeeding decisions, once the resultant of a
decision is true.
Negative logic
The flow of processing continues through the module when the
resultant of a decision is false.
Multiple If/Then/Else Instructions
Algorithm
Flowchart
A
F If PayType = T
Hourly
F If Hours > T
40
Pay = Salary
B
Using Straight through Logic
processed.
in data validation
Using Straight through Logic
Age Charges
Age < 16 7
Age >=16 & Age <65 10
Age > = 65 5
Using Straight through Logic
Algorithm
If Age < 16 then
Charge = 7
Charge = 10
Charge = 5
Using Straight through Logic Flowchart
A
If Age T
< 16
Charge = 7
F
If Age >16 T
and Age < 65
Charge = 10
F
If Age T
>= 65
Charge = 5
F
B
Positive Logic
of negative terms.
next decision.
Negative Logic Flowchart
Logic Conversion
Condition
T T T T F F F F
1
List of Condition
T T F F T T F F All possible
Conditions 2 Combinations
Condition of T & F
T F T F T F T F
3
Action 1 X X
List of Consequences
Action 2 X X X X
Actions
Action 3 X X
Flowchart from Decision Table
The last payment to the account was made in the last 30 days.
Purchase
T T T T F F F F
< $100
Last
List of Payment
T T F F T T F F All possible
Conditions within 30
Combinations
days of T & F
Balance <
T F T F T F T F
$1,000
Credit ok X X
Refer to
List of Credit
X X X Consequences
Actions Departme
nt
Credit
X X X
Denied
Decision Table Example
Control
Module
Input Processing Output
Reference
1. Amount of 1. Enter input Read 1. Amount of
purchase data Calc purchase
2. Resale number 2. Calculate Calc 2. Resale #
( If 0 then a discount Print 3. Amount owed
retail 3. Calculate
customer) amount owed
4. Print output
information
Fantastic Floral Company – Coupling Diagram
PA Control AO
PA RN AO PA
RN RN
PA – Purchase Amount
RN – Resale Number
AO – Amount Owed
Fantastic Floral Company – Data Dictionary
Exit Stop
1. Print PA, RN
And AO
Print PA,
2. Exit None Prints Output
RN and
AO
Exit Return
Objectives:
Develop problems using the loop logic structure in conjunction
with the decision and sequential logic structures.
Use of seven problem solving tools to solve a problem using
the loop logic structure.
Use counters and accumulators in a problem solution.
Use nested loop instructions to develop a problem solution
Distinguish the different uses of three types of loop logic
structures.
Use recursion in s simple problem.
The Loop logic structure
Repeat/Until loop
Repeats instructions while a condition is false or until a condition
is true.
Flowchart:
A
While F
<Condition(s)>
Instruction
Instruction
B
Decision equivalent to While/WhileEnd
Algorithm Flowchart
A
If <Condition(s) >
then
While F
Instruction <Condition(s)>
T
Instruction T
Instruction
Go to 100
Instruction
Go To
B
PIAT (Putting It All Together)
Flowchart A
Repeat
Instruction
Instruction
Until
F <Condition(s)>
B
Decision equivalent to Repeat/Until
PIAT (Putting It All Together)
A
Flowchart
C
Begin End
Step
Instruction
Instruction
D
Decision Equivalent to Automatic Counter Loop
While/WhileEnd loop Equivalent of the
Automatic counter loop
Repeat/Until loop equivalent to Automatic
Counter Loop
Rules for Automatic Counter Loop
For incremental loop:
When the computer executes the loop instruction it sets the counter
equal to the beginning number.
When the computer executes the loop end, it increments the counter.
When the counter is less than or equal to the ending number, the
processing continues at the instruction that follow the loop
instruction. When the counter is greater than the ending number, the
processing continues at the instruction that follows the loop-end
instruction.
For decremented loop:
Counter is decremented instead of incremented at the end of the
loop.
When the counter is greater than or equal to the ending value, the
loop continues, instead of when it is less than or equal to the ending
value when incrementing.
PIAT (Putting It All Together)
The inner loop does not have the same types of loop
module.
loop structures.
Example of Recursion
Objectives:
another option.
The Case Logic Structure