Title: Programming Logic and Design Fifth Edition, Comprehensive
1Programming Logic and Design Fifth Edition,
Comprehensive
- Chapter 2
- Understanding Structure
2Objectives
- 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
3Understanding 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
4Understanding Unstructured Spaghetti Code
(continued)
- Example College Admissions
- Admit students who score gt 90 on admissions test
if upper 75 percent of high-school graduating
class - Admit students who score gt 80 on test if upper
50 percent of high-school graduating class - Admit students who score gt 70 on admission test
if upper 25 percent of high-school graduating
class
5Understanding Unstructured Spaghetti Code
(continued)
Figure 2-2 Spaghetti code example
6Understanding 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
- Loop
- Repeat actions based on answer to a question
7Understanding the Three Basic Structures
(continued)
Figure 2-3 Sequence structure
8Understanding the Three Basic Structures
(continued)
Figure 2-4 Selection structure
9Understanding the Three Basic Structures
(continued)
- Dual-alternative if contains two alternatives
if someCondition is true then do
oneProcess else do theOtherProcess
10Understanding the Three Basic Structures
(continued)
- Single-alternative if contains one alternative
Figure 2-5 Single-alternative selection structure
11Understanding the Three Basic Structures
(continued)
- Single-alternative if
- Else clause is not required
- Null case situation where nothing is done
-
if employee belongs to dentalPlan then deduct
40 from employeeGrossPay
12Understanding 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
13Understanding the Three Basic Structures
(continued)
while testCondition continues to be true do
someProcess while quantityInInventory remains
low continue to orderItems
14Understanding 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 - The endif statement ends an if-then-else
structure - The endwhile ends a loop structure
15Understanding the Three Basic Structures
(continued)
Figure 2-7 Structured flowchart and pseudocode
16Understanding the Three Basic Structures
(continued)
- Any individual task or step in a structure can be
replaced by a structure - Nesting placing one structure within another
- Indent the nested structures statements
- Block group of statements that execute as a
single unit
17Understanding the Three Basic Structures
(continued)
Figure 2-8 Flowchart and pseudocode showing a
sequence nested within a
selection
18Understanding the Three Basic Structures
(continued)
Figure 2-9 Selection in a sequence within a
selection
19Understanding the Three Basic Structures
(continued)
Figure 2-10 Flowchart and pseudocode for loop
within selection within sequence within selection
20Understanding 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
21Using 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 - First ask a question
- Take action based on the answer
- Return to ask the question again
22Using the Priming Read (continued)
Figure 2-12 Unstructured flowchart of a
number-doubling program
23Using the Priming Read (continued)
- Structured but nonfunctional loop
Figure 2-15 Structured, but nonfunctional,
flowchart of
number-doubling problem
24Using the Priming Read (continued)
- Functional but nonstructured loop
Figure 2-16 Functional, but nonstructured,
flowchart
25Using the Priming Read (continued)
- Functional and structured loop
Figure 2-17 Functional, structured flowchart and
pseudocode for the number-doubling problem
26Using the Priming Read (continued)
- Priming read sets up the process so the loop can
be structured - To analyze a flowcharts structure, try writing
pseudocode for it
start get inputNumber while not
eof calculatedAnswer inputNumber 2 print
calculatedAnswer get inputNumber endwhile stop
27Using the Priming Read (continued)
- What is wrong with this design?
Figure 2-18 Structured but incorrect solution to
the number-doubling problem
28Understanding the Reasons for Structure
- Provides clarity
- Professionalism
- Efficiency
- Ease of maintenance
- Supports modularity
29Recognizing 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
30Recognizing Structure (continued)
- Is this flowchart structured?
Figure 2-22 Example 2
31Recognizing Structure (continued)
- Is this flowchart structured?
Figure 2-23 Example 3
32Recognizing Structure (continued)
- Single process like A is part of an unacceptable
structure - At least the beginning of a sequence structure
Figure 2-24 Untangling Example 3, first step
33Recognizing 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
34Recognizing Structure (continued)
- Pull up on the flowline from the left side of B
Figure 2-26 Untangling Example 3, third step
35Recognizing Structure (continued)
- Next, pull up the flowline on the right side of B
Figure 2-27 Untangling Example 3, fourth step
36Recognizing 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
37Recognizing Structure (continued)
- Now pull up the flowline on the right side of D
Figure 2-29 Untangling Example 3, sixth step
38Recognizing Structure (continued)
- Bring together the loose ends of D and of B
Figure 2-30 Finished flowchart and pseudocode
for untangling Example 3
39Three Special Structures CASE, DO-WHILE, and
DO-UNTIL
- Many languages allow three additional structures
- The case structure
- The do-while structure
- The do-until 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
40Three Special Structures Case, Do While, and Do
Until (continued)
- Using nested if-then-else for multiple
alternatives
Figure 2-31 Flowchart and pseudocode of tuition
decisions
41Three Special Structures Case, Do While, and Do
Until (continued)
- Using a case structure for multiple alternatives
Figure 2-32 Flowchart and pseudocode of case
structure
42Three Special Structures Case, Do While, and Do
Until (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
43Three Special Structures Case, Do While, and Do
Until (continued)
- do-while executes as long as the questions
answer is Yes or True - do-until executes as long as the questions
answer is No or False
do wash a dish until all dishes are washed
do wash a dish while more dishes remain to be
washed
44Three Special Structures Case, Do While, and Do
Until (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
45Three Special Structures Case, Do While, and Do
Until (continued)
Figure 2-35 Flowchart and pseudocode for
do-while loop
46Three Special Structures Case, Do While, and Do
Until (continued)
Figure 2-36 Flowchart and pseudocode for
sequence followed by while loop
47Three Special Structures Case, Do While, and Do
Until (continued)
- How can this design be made structured?
Figure 2-37 Unstructured loop
48Three Special Structures Case, Do While, and Do
Until (continued)
- Repeat the needed step to enforce structure
Figure 2-38 Sequence and structured loop that
accomplish the same
tasks as Figure 2-37
49Summary
- 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
50Summary (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 posttest loops that
ask the question last - do-while and do-until loop statements are always
executed at least once - Posttest loop can be replaced by a sequence
followed by a while loop