programming logic and design seventh edition chapter 3 understanding structure

47
Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Upload: rudolf-hill

Post on 17-Dec-2015

225 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Programming Logic and DesignSeventh Edition

Chapter 3Understanding Structure

Page 2: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

ObjectivesIn this chapter, you will learn about:

• The three basic control structures:– Sequence

– Selection

– Repetition ( aka looping and iteration )

• The need for structure

• The features of unstructured spaghetti code

• Recognizing structure

• Structuring and modularizing unstructured logic

• Using a priming input to structure a program2

Page 3: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

3

Three Basic Control StructuresSequence, Selection, Repetition/Iteration/Looping

Page 4: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

RAPTOR Symbols

4

Sequence Control Structures

Selection and RepetitionControl Structures

Page 5: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Understanding Unstructured Spaghetti Code

• Spaghetti code

– Logically snarled program statements

– Can be the result of poor program design

– Programs often work but are difficult to read and maintain

– Convoluted logic usually requires more code

• Unstructured programs

– Do not follow the rules of structured logic

• Structured programs

– Do follow rules of structured logic

• RAPTOR does not permit you to draw an unstructured flowchart

5

Page 6: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

6

Figure 3-1 Spaghetti code logic for washing a dog

Page 7: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Understanding the Three Basic Structures

• Structure– A Control Structure is a basic unit of programming logic

– Sequence• Perform a single action ( input, output, assign, … )

– Selection ( aka decision )• Ask a question, take one of two actions

• Dual-alternative or single-alternative if's

– Loop ( aka repetition/iteration )• Repeat actions based on answer to a question

7

Page 8: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

8

Understanding the Three Basic Structures (continued)

Figure 3-2 Sequence

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 being executed in sequence ( or sequentially ). To the right of that, you see 3 selection structures in a sequence.

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.

Start

NoYes

NoYes

NoYes

End

Page 9: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

9

Understanding the Three Basic Structures (continued)

Figure 3-3 Selection structure

no yes

question usually referred to as the condition

statementstatement

Page 10: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

10

Understanding the Three Basic Structures (continued)

• Dual-alternative if– Contains two alternatives– If-then-else structure

if someCondition [is true] then

do statement_1

else

do statement_2

endif

statements

Pseudocode

keywords:

if, then, do, else,

endif

Page 11: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Understanding the Three Basic Structures (continued)

• Single-alternative if– Else clause is not required

– If the condition does not evaluate to true, the statement(s) are not executed and control passes to the next structure

• null case– Situation where nothing is done

11

if employee belongs to dentalPlan then

do deduct $40 from employeeGrossPay

endif

Page 12: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

12

Understanding the Three Basic Structures (continued)

Figure 3-4 Single-alternative selection structure

Page 13: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

13

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 … do (pre-test) or do … while (post-test)

– RAPTOR lets you put the test anywhere you like!

Page 14: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

14

Understanding the Three Basic Structures (continued)

Figure 3-5 Loop structure (pre-test form – while … do)

Connector

When using drawing software

such as Visio

Page 15: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

15

Understanding the Three Basic Structures (continued)

• Loop structure [ pre-test loop ]

while testCondition continues to be true

do someStatement

endwhile

while count less than 11

do printCount() //module call

do add 1 to count

endwhile

Page 16: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

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

• 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 loop structure

16

Page 17: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

17

Understanding the Three Basic Structures (continued)

Figure 3-6 Structured flowchart and pseudocode with three stacked structures

Connector

Page 18: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Understanding 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 structure’s statements

• Block [ aka compound statement ]– Group of statements that execute as a single unit– Java uses { } to enlcose these

18

Page 19: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

19

Understanding the Three Basic Structures (continued)

Figure 3-7 Flowchart and pseudocode showing nested structures[ sequence nested within a selection ]

block or compound statement

Page 20: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

20

Understanding the Three Basic Structures (continued)

Figure 3-8 Flowchart and pseudocode showing nested structuresa loop nested within a sequence, nested within a selection

entry point and exit point for a structure

structures have ONE of each

Page 21: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

21

Understanding the Three Basic Structures (continued)

Figure 3-9 Flowchart and pseudocode for selection structure with nested sequence and loop structures

Page 22: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

22

Understanding the Three Basic Structures (continued)

• Structured programs have the following characteristics:

– Include only combinations of the three basic structures

– Each of the structures has a single entry point and a single exit point

– Structures can be stacked or connected to one another only at their entry or exit points

– Selection and Loop structures can have nested structures

Page 23: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Using a Priming Input to Structurea Program

• Priming read (or priming input)

– Reads the first input data record outside of the loop that reads the rest of the records

– Helps keep the program structured

– Note: it is not always necessary to do this…

• 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

23

Page 24: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Using a Priming Input to Structure a Program (continued)

24

Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling problem

Page 25: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Using a Priming Input to Structure a Program (continued)

Programming Logic & Design, Sixth Edition 25

Figure 3-16 Functional but unstructured flowchart

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.

Page 26: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

26

Using a Priming Input to Structure a Program (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 //priming read

while not eofcalculatedAnswer = inputNumber * 2print calculatedAnswerget inputNumber

endwhilestop

Page 27: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

27

Figure 3-17 Functional, structured flowchart for the number-doubling problem

Page 28: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Programming Logic & Design, Sixth Edition 28

Figure 3-18 Structured but incorrect solution to the number-doubling problem

Page 29: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Understanding the Reasons for Structure

• Clarity

• Professionalism

• Efficiency

• Ease of maintenance

• Supports modularity

29

Page 30: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

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

30

Page 31: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Flowchart Status

• Flowcharts fall in to one of the 3 following categories:

– structured AND functional– structured but NOT functional– functional but NOT structured

• These categories will be presented over the next several slides.

31

Page 32: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Programming Logic & Design, Sixth Edition 32

Recognizing Structure (continued)

Figure 3-20 Example 2

What are the structures?

Are they: stacked? nested?

Are they: structured? functional?

Page 33: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

33

Recognizing Structure (continued)

Figure 3-21 Example 3

Functional butnot

Structured

This is a no no!

Page 34: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

34

Figure 3-22 Untangling Example 3, first step

• Single process like G is part of an acceptable structure– At least the beginning of a sequence structure

Recognizing Structure (continued)

Page 35: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

35

• H begins a selection structure – Sequences never have decisions in them– Logic never returns to G

Figure 3-23 Untangling Example 3, second step

Recognizing Structure (continued)

Page 36: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

36

• Pull up on the flowline from the left side of H

Figure 3-24 Untangling Example 3, third step

Recognizing Structure (continued)

Page 37: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

37

• Next, pull up the flowline on the right side of H

Figure 3-25 Untangling Example 3, fourth step

Recognizing Structure (continued)

Page 38: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

38

• Pull up the flowline on the left side of I and untangle it from the B selection by repeating J

Figure 3-26 Untangling Example 3, fifth step

Recognizing Structure (continued)

Page 39: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

39

• Now pull up the flowline on the right side of I

Figure 3-27 Untangling Example 3, sixth step

Recognizing Structure (continued)

Page 40: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

40

• Bring together the loose ends of I and of H

Figure 3-28 Finished flowchart and pseudocode for untangling Example 3

Recognizing Structure (continued)

Page 41: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Structuring and ModularizingUnstructured Logic

• Dog-washing process– Unstructured– Can be reconfigured to be structured

• First step simple sequence

41

Figure 3-29 Washing the dog, part 1

Page 42: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Structuring and ModularizingUnstructured Logic (continued)

• After the dog runs away– Catch the dog and determine whether he runs away again– A loop begins

42

Figure 3-30 Washing the dog, part 2

Page 43: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Programming Logic & Design, Sixth Edition 43

Figure 3-31 Washing the dog, part 3

Page 44: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Programming Logic & Design, Sixth Edition 44

Figure 3-33 Structured dog-washing flowchart and pseudocode

Page 45: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Programming Logic & Design, Sixth Edition 45

Figure 3-34 Modularized version of the dog-washing program

Page 46: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Summary

• Spaghetti code– Snarled program logic

• Three basic structures– Sequence, selection, and loop– Combined by stacking and nesting

• Priming read– Statement that reads the first input data record

46

Page 47: Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure

Summary (continued)

• Structured techniques promote: – Clarity– Professionalism– Efficiency– Modularity

• Flowchart can be made structured by untangling

47