Fifth Edition, Comprehensive Chapter 2 Understanding Structure

Programming Logic and
Design
Fifth Edition, Comprehensive
Chapter 2
Understanding Structure
Objectives
• Learn about the features of unstructured spaghetti
code
• Understand the three basic structures: sequence,
selection, and loop
• Use a priming read
• Appreciate the need for structure
• Recognize structure
– Sequence, Selection, Repetition
Programming Logic and Design, Fifth Edition, Comprehensive
2
Understanding Unstructured
Spaghetti Code
• Spaghetti code:
– logically snarled program statements
– Can be the result of poor program design
• Spaghetti code programs often work, but are difficult
to read and maintain
• Convoluted logic usually requires more code
Programming Logic and Design, Fifth Edition, Comprehensive
3
Understanding Unstructured
Spaghetti Code (continued)
• Example: College Admissions
[ score/percentile ]
– Admit students who score >= 90 on admissions test if upper 75
percent of high-school graduating class
– Admit students who score >= 80 on test if upper 50 percent of
high-school graduating class
– Admit students who score >= 70 on admission test if upper 25
percent of high-school graduating class
Programming Logic and Design, Fifth Edition, Comprehensive
4
Understanding Unstructured
Spaghetti Code (continued)
Figure 2-2 Spaghetti code example
Programming Logic and Design, Fifth Edition, Comprehensive
5
Understanding the Three Basic
Structures
• Structure: basic unit of programming logic
• Any program can be constructed from only three basic
types of structures
– Sequence
• Perform actions in order
• No branching or skipping any task
– Selection (decision)
• Ask a question, take one of two actions
• Dual-alternative or single-alternative
– Looping ( iteration, repetition )
• Repeat actions based on answer to a question
Programming Logic and Design, Fifth Edition, Comprehensive
6
Understanding the Three Basic
Structures (continued)
• Sequence structure
Figure 2-3 Sequence structure
Programming Logic and Design, Fifth Edition, Comprehensive
7
Understanding the Three Basic
Structures (continued)
• Selection structure
Figure 2-4 Selection structure
Programming Logic and Design, Fifth Edition, Comprehensive
8
Understanding the Three Basic
Structures (continued)
• Dual-alternative if: contains two alternatives
if someCondition is true then
do oneProcess
else
do theOtherProcess
Programming Logic and Design, Fifth Edition, Comprehensive
9
Understanding the Three Basic
Structures (continued)
• Single-alternative if: contains one alternative
Figure 2-5 Single-alternative selection structure
Programming Logic and Design, Fifth Edition, Comprehensive
10
Understanding the Three Basic
Structures (continued)
• Single-alternative if
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
• Else clause is not required
• Null case: situation where nothing is done
Programming Logic and Design, Fifth Edition, Comprehensive
11
Understanding the Three Basic
Structures (continued)
• Loop structure
– Repeats a set of actions based on the answer to a question
– Also called repetition or iteration
– Question is asked first in the most common form of loop
Figure 2-6 Loop structure
Programming Logic and Design, Fifth Edition, Comprehensive
12
Understanding the Three Basic
Structures (continued)
• Loop structure
while testCondition continues to be true do
someProcess
while quantityInInventory remains low do
continue to orderItems
Programming Logic and Design, Fifth Edition, Comprehensive
13
Understanding the Three Basic
Structures (continued)
• All logic problems can be solved using only these three
structures
• Structures can be combined in an infinite number of ways
• Stacking: attaching structures end-to-end
• End-structure statements
– Indicate the end of a structure
– endif: ends an if-then-else structure
– endwhile: ends a loop structure
Programming Logic and Design, Fifth Edition, Comprehensive
14
Understanding the Three Basic
Structures (continued)
Figure 2-7 Structured flowchart and pseudocode
Programming Logic and Design, Fifth Edition, Comprehensive
15
Understanding the Three Basic
Structures (continued)
• Stacking: attaching structures end-to-end
• Nesting: placing one structure within another
– Indent the nested structure’s statements
• Block: group of statements that execute as a
single unit ( compound statement )
Programming Logic and Design, Fifth Edition, Comprehensive
16
Understanding the Three Basic
Structures (continued)
Figure 2-8 Flowchart and pseudocode showing a sequence
nested within a selection
Programming Logic and Design, Fifth Edition, Comprehensive
17
Understanding the Three Basic
Structures (continued)
Figure 2-9 Selection in a sequence within a selection
Programming Logic and Design, Fifth Edition, Comprehensive
18
Understanding the Three Basic
Structures (continued)
Figure 2-10 Flowchart and pseudocode for loop within selection within
sequence within selection
Programming Logic and Design, Fifth Edition, Comprehensive
19
Understanding the Three Basic
Structures (continued)
• Each structure has one entry and one exit point
• Structures attach to others only at entry or exit points
Figure 2-11 The three structures
Programming Logic and Design, Fifth Edition, Comprehensive
20
Using the Priming Read
•
Priming read (or priming input):
–
–
–
•
Reads the first input data record
Outside the loop that reads the rest of the records
Helps keep the program structured
Analyze a flowchart for structure one step at a time
Watch for unstructured loops that do not follow this order:
1. First ask a question
2. Take action based on the answer
3. Return to ask the question again
Programming Logic and Design, Fifth Edition, Comprehensive
21
Using the Priming Read (continued)
• Unstructured loop
Figure 2-12 Unstructured flowchart of a number-doubling program
Programming Logic and Design, Fifth Edition, Comprehensive
22
Using the Priming Read (continued)
• Structured but non-functional loop
Figure 2-15 Structured, but nonfunctional, flowchart of
number-doubling problem
Programming Logic and Design, Fifth Edition, Comprehensive
23
Using the Priming Read (continued)
• Functional but non-structured loop
Figure 2-16 Functional, but nonstructured, flowchart
Programming Logic and Design, Fifth Edition, Comprehensive
24
Using the Priming Read (continued)
• Functional and structured loop
Figure 2-17 Functional, structured flowchart and pseudocode for the
number-doubling problem
Programming Logic and Design, Fifth Edition, Comprehensive
25
Using the Priming Read (continued)
• Priming read sets up the process so the loop can be
structured
• To analyze a flowchart’s structure, try writing
pseudocode for it
start
get inputNumber
while not eof
calculatedAnswer = inputNumber * 2
print calculatedAnswer
get inputNumber
endwhile
stop
Programming Logic and Design, Fifth Edition, Comprehensive
26
Using the Priming Read (continued)
• What is wrong with this design?
Figure 2-18 Structured but incorrect solution to the number-doubling problem
Programming Logic and Design, Fifth Edition, Comprehensive
27
Understanding the Reasons for
Structure
• Provides clarity
• Professionalism
• Efficiency
• Ease of maintenance
• Supports modularity
• Also,very importantly, you cannot write code that is not
using "structured" logic without the use of a goto
statement!
Programming Logic and Design, Fifth Edition, Comprehensive
28
Recognizing Structure
• Any set of instructions can be expressed in
structured format
• Any task to which you can apply rules can be
expressed logically using sequence, selection, loop
• It can be difficult to detect whether a flowchart is
structured
Programming Logic and Design, Fifth Edition, Comprehensive
29
Recognizing Structure (continued)
• Is this flowchart structured?
Figure 2-22 Example 2
Programming Logic and Design, Fifth Edition, Comprehensive
30
Recognizing Structure (continued)
• Is this flowchart structured?
Figure 2-23 Example 3
Programming Logic and Design, Fifth Edition, Comprehensive
31
Recognizing Structure (continued)
• A is the start of some type of structure
– At least the beginning of a sequence structure
Figure 2-24 Untangling Example 3, first step
Programming Logic and Design, Fifth Edition, Comprehensive
32
Recognizing Structure (continued)
• B begins a selection structure
– Sequences never have decisions in them
– Logic never returns to B
Figure 2-25 Untangling Example 3, second step
Programming Logic and Design, Fifth Edition, Comprehensive
33
Recognizing Structure (continued)
• Pull up on the flowline from the left side of B
Figure 2-26 Untangling Example 3, third step
Programming Logic and Design, Fifth Edition, Comprehensive
34
Recognizing Structure (continued)
• Next, pull up the flowline on the right side of B
Figure 2-27 Untangling Example 3, fourth step
Programming Logic and Design, Fifth Edition, Comprehensive
35
Recognizing Structure (continued)
• Pull up the flowline on the left side of D and untangle
it from the B selection by repeating C
Figure 2-28 Untangling Example 3, fifth step
Programming Logic and Design, Fifth Edition, Comprehensive
36
Recognizing Structure (continued)
• Now pull up the flowline on the right side of D
Figure 2-29 Untangling Example 3, sixth step
Programming Logic and Design, Fifth Edition, Comprehensive
37
Recognizing Structure (continued)
• Bring together the loose ends of D and of B
Figure 2-30 Finished flowchart and pseudocode for untangling Example 3
Programming Logic and Design, Fifth Edition, Comprehensive
38
Three Special Structures
•
Languages often allow three additional structures:
– The
– The
– The
case
do-while
do-until
structure
structure
structure
• CASE Structure:
– Decisions with more than two alternatives
– Tests a variable against a series of values and takes action
based on a match
– Nested if-then-else statements will do what a case
structure does
Programming Logic and Design, Fifth Edition, Comprehensive
39
Three Special Structures (continued)
• Using nested if-then-else for multiple
alternatives
Figure 2-31 Flowchart and pseudocode of tuition decisions
Programming Logic and Design, Fifth Edition, Comprehensive
40
Three Special Structures (continued)
• Using a case structure for multiple alternatives
Figure 2-32 Flowchart and pseudocode of case structure
Programming Logic and Design, Fifth Edition, Comprehensive
41
Three Special Structures (continued)
• do-while and do-until loops
– Question is asked at the end of the loop structure
– Loop statements always used at least once
Figure 2-33 The while loop, which
is a pretest loop
Figure 2-34 Structure of a do-while or
do-until loop, which are posttest loops
Programming Logic and Design, Fifth Edition, Comprehensive
42
Three Special Structures (continued)
• do-while: executes as long as the question’s
answer is Yes or True ( do while true )
• do-until: executes as long as the question’s
answer is No or False ( do until true )
do
wash a dish
until all dishes are washed
do
wash a dish
while more dishes remain to be washed
Programming Logic and Design, Fifth Edition, Comprehensive
43
Three Special Structures (continued)
• while loop with question at beginning is called a
pretest loop
• do-while and do-until with question at end are
called posttest loops
– Posttest loop can be replaced with a sequence
followed by a pretest while loop
pay a bill
while there are more bills to pay
pay a bill
endwhile
Programming Logic and Design, Fifth Edition, Comprehensive
44
Three Special Structures (continued)
Figure 2-35 Flowchart and pseudocode for do-while loop
Programming Logic and Design, Fifth Edition, Comprehensive
45
Three Special Structures (continued)
Figure 2-36 Flowchart and pseudocode for sequence followed by while loop
Programming Logic and Design, Fifth Edition, Comprehensive
46
Three Special Structures (continued)
• How can this design be made structured?
Figure 2-37 Unstructured loop
Programming Logic and Design, Fifth Edition, Comprehensive
47
Three Special Structures (continued)
• Repeat the needed step to enforce structure
Figure 2-38 Sequence and structured loop that accomplish the
same tasks as Figure 2-37
Programming Logic and Design, Fifth Edition, Comprehensive
48
Summary
• Spaghetti code: snarled program logic
• Three basic structures: sequence, selection, loop
– Combined by stacking and nesting
• Priming read: statement that reads the first input
data record
• Structured techniques promote:
– clarity, professionalism, efficiency, and modularity
• Flowchart can be made structured by untangling
Programming Logic and Design, Fifth Edition, Comprehensive
49
Summary (continued)
• case structure: questions with multiple alternatives
• while loop: a pretest loop asks the question first
• while loop statements never execute if the answer is
No
• do-while and do-until loops: post-test loops that
ask the question last
• do-while and do-until loop statements are always
executed at least once
– Post-test loop can be replaced by a sequence followed
by a while loop
Programming Logic and Design, Fifth Edition, Comprehensive
50