ch03

advertisement
Programming Logic and Design
Seventh Edition
Chapter 3
Understanding Structure
Objectives
In this chapter, you will learn about:
• The three basic control structures:
– Sequence
– Selection
– Repetition ( aka looping and iteration )
• The need for structure
• Using a priming input
2
Three Basic Control Structures
Sequence, Selection, Repetition/Iteration/Looping
Connector
Use where
flowlines
come
together in
a flowchart
3
RAPTOR Flowchart Symbols
Sequence
Control
Structures
Selection and
Repetition
Control
Structures
4
Understanding the Three Basic Control Structures (continued)
There is a distinction between sequence and a
sequence structure. The book doesn't make this
distinction which is unfortunate.
On the right, you see 3 sequence structures that will
execute in sequence (or sequentially). To the right of
that, you see 3 selection structures that will execute
in a sequence (sequentiall).
You enter a control structure through an entry point
and exit from an exit point.
Control structures can be stacked and nested.
Stacked control structures are always executed in
sequence.
Figure 3-2
Sequence control structure
Sequential execution of control structures
5
Understanding the Three Basic Control Structures (continued)
expression
usually referred to as the
condition
null case
No structure
to be executed
Figure 3-4 Single-alternative if selection structure
6
Understanding the Three Basic Control Structures (continued)
• Single-alternative if
– Else clause is not required
– If the condition does not evaluate to true, the structure(s) are
not executed and control passes to the next structure
• null case
– Situation where nothing is done
– This would be the "empty" else branch of a selection structure
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
endif
7
Understanding the Three Basic Control Structures (continued)
no
structure
yes
structure
Figure 3-3 Dual-alternative if selection structure
8
Understanding the Three Basic Control Structures (continued)
• Dual-alternative if
– Contains two alternatives
– If-then-else structure
if someCondition [is true] then
Pseudocode
keywords:
if, then, else,
endif
statement_1
structures
else
statement_2
endif
9
Understanding the Three Basic Control Structures (continued)
•
•
•
•
•
Some languages have an "else if" keyword
The spelling of the keyword in languages varies (elseif / elsif / elif)
Python uses the elif keyword
Java and C++ do not have an "else-if" keyword
Usage:
if x == 1 then
print 1
elif x == 2 then
print 2
... (additional elif statements)
else
print "no match for x"
endif
10
Understanding the Three Basic Structures (continued)
• Loop structure
– Repeats a set of actions based on the answer to a question
• Loop body
– Also called repetition or iteration
– Question is asked first in the most common form of a loop
– while (pre-test)
or
do … while (post-test)
– RAPTOR lets you put the test anywhere you like!
11
Understanding the Three Basic Control Structures (continued)
Entry Point
Connector
When using
drawing software
such as Visio
Figure 3-5
12
Loop structure (pre-test form – while loop)
Understanding the Three Basic Control Structures (continued)
• Loop structure [ pre-test loop ]
while testCondition is true
do someStatement
endwhile
while count < 11
printCount()
count += 1
endwhile
13
//module call
Understanding the Three Basic Control Structures (continued)
• All logic problems can be solved using only these three control structures
• Structures can be combined in an infinite number of ways
• Stacking
– Attaching structures end-to-end at their entry/exit points
• Nesting
– discussed a few slides later…
• End-structure statements
– Indicate the end of a structure
– endif ends an if-then-else structure
– endwhile ends a pre-test loop structure
– enddo ends a post-test loop structure
14
Understanding the Three Basic Control Structures (continued)
Figure 3-6 Structured flowchart and pseudocode with three stacked structures
15
Understanding the Three Basic Control Structures (continued)
• Any individual task or step can be inserted by
creating a new structure or an existing structure can
be replaced by a different structure
• Nesting
– Placing one structure within another
– Indent the nested structure’s statements
• Block [ aka compound statement ]
– Group of statements that execute as a single unit
– Java and C++ use { } to enlcose these
– Python relies on indentation of the block (suite)
16
Understanding the Three Basic Control Structures (continued)
block or compound
statement
Figure 3-7 Flowchart and pseudocode showing nested structures
[ sequence nested within a selection ]
17
Understanding the Three Basic Control Structures (continued)
entry point
and
exit point
for a structure
structures
have ONE of
each
Figure 3-8 Flowchart and pseudocode showing nested structures
a loop nested within a sequence, nested within a selection
18
Understanding the Three Basic Structures (continued)
selection
Note:
The diamond
flowchart symbol
is used for both
selection and
loop structures.
loop
How do you
know if you are
looking at a
selection or loop
structure?
A selection structure
only has flowlines
merging at the exit point
for the structure
Figure 3-9 Flowchart and pseudocode for
selection structure with nested sequence and loop structures
19
A loop structure will
always have flowlines
merging above the test
expression and the body
of the loop
Understanding the Three Basic Control Structures (continued)
• Rules for creating a "structured" flowchart
– Include only combinations of the three basic control
structures (sequence, selection, loop)
– Each of the structures has a single entry point and a single
exit point
– Structures can be stacked (connected) to one another
only at their entry and exit points
– Ideally whenever flowchart lines come together a
connector is used
(if drawn manually)
– Selection and Loop structures can have nested structures
20
Using a Priming Input
• Priming input (or priming read)
– Does the first input operation outside of the loop that
inputs the rest of the data
– Inside the body of the loop the input operation is usually
the last operation performed in the body of the loop
– Note: it is not always necessary to do this…
Some languages can “look ahead” to see if there is any
data that can be input
21
Using a Priming Input (continued)
RAPTOR would
let you do this
because there
are languages
that support a
“mid-test”
condition.
Our book only
considers pre-test
and post-test
loops as
structured.
Figure 3-16
Programming Logic & Design, Sixth Edition
22
Using a Priming Input (continued)
• Priming input sets up the process so the loop can be
structured
• To analyze a flowchart’s structure, try writing
pseudocode for it
start
get inputNumber
//priming input
while not eof
calculatedAnswer = inputNumber * 2
print calculatedAnswer
get inputNumber
endwhile
stop
23
A priming input is
OUTSIDE the loop
structure.
It precedes the
structure.
The input structure
INSIDE the loop is
usually the last
structure nested in
the loop.
Figure 3-17
number-doubling problem
24
Figure 3-18 Structured but incorrect solution to the number-doubling problem
Programming Logic & Design, Sixth Edition
25
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, and
loop control structures
26
Flowchart Status
• Flowcharts fall into one of the 2 following categories:
1. structured
2. unstructured
27
Recognizing Structure (continued)
This is a no
no!
Understandable
but not
Structured
Figure 3-21 Example 3
28
Recognizing Structure (continued)
What are the
structures?
Are they:
stacked?
nested?
Are they:
structured?
Figure 3-20 Example 2
Programming Logic & Design, Sixth Edition
29
Recognizing Structure (continued)
Nested selection structures in RAPTOR
Programming Logic and Design, Seventh Edition
30
Summary
• Spaghetti code
– Snarled program logic
• Three basic control structures
– Sequence, selection, and looping
– Combined by stacking and/or nesting
• Priming input
– Statement that reads the first input data record
31
Download