CHAPTER 2: Understanding Structure. Objectives 2 Learn about the features of unstructured spaghetti...
-
Upload
derrick-gibbs -
Category
Documents
-
view
365 -
download
8
Transcript of CHAPTER 2: Understanding Structure. Objectives 2 Learn about the features of unstructured spaghetti...
![Page 1: CHAPTER 2: Understanding Structure. Objectives 2 Learn about the features of unstructured spaghetti code Understand the three basic structures: sequence,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/1.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/2.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/3.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/4.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/5.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/6.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/7.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/8.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/9.jpg)
Stacking
![Page 10: CHAPTER 2: Understanding Structure. Objectives 2 Learn about the features of unstructured spaghetti code Understand the three basic structures: sequence,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/10.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/11.jpg)
Nesting
![Page 12: CHAPTER 2: Understanding Structure. Objectives 2 Learn about the features of unstructured spaghetti code Understand the three basic structures: sequence,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/12.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/13.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/14.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/15.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/16.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/17.jpg)
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,](https://reader033.fdocuments.net/reader033/viewer/2022052118/56649f495503460f94c6b97a/html5/thumbnails/18.jpg)
Understanding the Reasons for Structure
Provides clarity Professionalism Efficiency Ease of maintenance Supports modularity