Structure

CHAPTER 2:
Understanding Structure
Objectives
2


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
Learn about three special structures: case, do-while, and
do-until
Spaghetti Code
3

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
Structure
4


Structure: basic unit of programming logic
Any program can be constructed from only three basic types of
structures

Sequence



Selection (decision)



Perform actions in order
No branching or skipping any task
Ask a question, take one of two actions
Dual-alternative or single-alternative
Loop

Repeat actions based on answer to a question
Structures
Sequence
Selection
Repeat
Selection

Dual-alternative if: contains two alternatives
if someCondition is true then
do oneProcess
else
do theOtherProcess

Single-alternative if
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
Loop

Loop structure
while testCondition continues to be true
do someProcess
while quantityInInventory remains low
continue to orderItems
Stacking




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
 The
endif statement ends an if-then-else structure
 The endwhile ends a loop structure
Stacking
Nesting




Any individual task or step in a structure
can be replaced by a structure
Nesting: placing one structure within
another
Indent the nested structure’s statements
Block: group of statements that execute
as a single unit
Nesting
IMPORTANT


Each structure has one entry and one exit point
Structures attach to others only at entry or exit
points
Using the Priming Read (continued)

Unstructured loop
Figure 2-12 Unstructured flowchart of a number-doubling program
Using the Priming Read (continued)

Structured but nonfunctional loop
Figure 2-15 Structured, but nonfunctional, flowchart of
number-doubling problem
Using the Priming Read (continued)

Functional but nonstructured loop
Figure 2-16 Functional, but nonstructured, flowchart
Using the Priming Read (continued)

Functional and structured loop
Figure 2-17 Functional, structured flowchart and pseudocode for the
number-doubling problem
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
Understanding the Reasons for Structure





Provides clarity
Professionalism
Efficiency
Ease of maintenance
Supports modularity