Download presentation
Presentation is loading. Please wait.
Published byDerrick Gibbs Modified over 9 years ago
1
CHAPTER 2: Understanding Structure
2
Objectives 2 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
3
Spaghetti Code 3 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
4
Structure 4 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
5
Structures Sequence Selection Repeat
6
Selection Dual-alternative if: contains two alternatives if someCondition is true then do oneProcess else do theOtherProcess Single-alternative if if employee belongs to dentalPlan then deduct $40 from employeeGrossPay
7
Loop Loop structure while testCondition continues to be true do someProcess while quantityInInventory remains low continue to orderItems
8
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
9
Stacking
10
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
11
Nesting
12
IMPORTANT Each structure has one entry and one exit point Structures attach to others only at entry or exit points
13
Using the Priming Read (continued) Unstructured loop Figure 2-12 Unstructured flowchart of a number-doubling program
14
Using the Priming Read (continued) Structured but nonfunctional loop Figure 2-15 Structured, but nonfunctional, flowchart of number-doubling problem
15
Using the Priming Read (continued) Functional but nonstructured loop Figure 2-16 Functional, but nonstructured, flowchart
16
Using the Priming Read (continued) Functional and structured loop Figure 2-17 Functional, structured flowchart and pseudocode for the number-doubling problem
17
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 it start get inputNumber while not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer get inputNumber endwhile stop
18
Understanding the Reasons for Structure Provides clarity Professionalism Efficiency Ease of maintenance Supports modularity
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.