chapter 2: understanding structure. objectives 2 learn about the features of unstructured spaghetti...

18
CHAPTER 2: Understanding Structure

Upload: derrick-gibbs

Post on 14-Jan-2016

365 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

CHAPTER 2:Understanding Structure

Page 2: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Objectives2

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

Page 3: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Spaghetti Code3

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

Page 4: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Structure4

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

Page 5: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Structures

Sequence

Selection

Repeat

Page 6: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Selection

Dual-alternative if: contains two alternativesif someCondition is true then

do oneProcess

else

do theOtherProcess

Single-alternative if

if employee belongs to dentalPlan then

deduct $40 from employeeGrossPay

Page 7: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Loop

Loop structure

while testCondition continues to be true

do someProcess

while quantityInInventory remains low

continue to orderItems

Page 8: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

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

Page 9: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Stacking

Page 10: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

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

Page 11: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Nesting

Page 12: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

IMPORTANT

Each structure has one entry and one exit point

Structures attach to others only at entry or exit points

Page 13: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Using the Priming Read (continued) Unstructured loop

Figure 2-12 Unstructured flowchart of a number-doubling program

Page 14: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Using the Priming Read (continued) Structured but nonfunctional loop

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

Page 15: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Using the Priming Read (continued) Functional but nonstructured loop

Figure 2-16 Functional, but nonstructured, flowchart

Page 16: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Using the Priming Read (continued) Functional and structured loop

Figure 2-17 Functional, structured flowchart and pseudocode for the number-doubling problem

Page 17: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

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 itstart

get inputNumberwhile not eof

calculatedAnswer = inputNumber * 2print calculatedAnswerget inputNumber

endwhilestop

Page 18: CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,

Understanding the Reasons for Structure

Provides clarity Professionalism Efficiency Ease of maintenance Supports modularity