490 likes | 1.09k Views
Programming Logic and Design, Fifth Edition, Comprehensive. 2. Objectives. Learn about the features of unstructured spaghetti codeUnderstand the three basic structures: sequence, selection, and loopUse a priming readAppreciate the need for structureRecognize structureLearn about three special structures: case, do-while, and do-until.
E N D
1. Programming Logic and Design Fifth Edition, Comprehensive Chapter 2
Understanding Structure
2. Programming Logic and Design, Fifth Edition, Comprehensive 2 Objectives 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. Programming Logic and Design, Fifth Edition, Comprehensive 3 Understanding Unstructured Spaghetti Code 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. Programming Logic and Design, Fifth Edition, Comprehensive 4 Understanding Unstructured Spaghetti Code (continued) Example: College Admissions
Admit students who score >= 90 on admissions test if upper 75 percent of high-school graduating class
Admit students who score >= 80 on test if upper 50 percent of high-school graduating class
Admit students who score >= 70 on admission test if upper 25 percent of high-school graduating class
5. Programming Logic and Design, Fifth Edition, Comprehensive 5 Understanding Unstructured Spaghetti Code (continued)
6. Programming Logic and Design, Fifth Edition, Comprehensive 6 Understanding the Three Basic Structures 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
7. Programming Logic and Design, Fifth Edition, Comprehensive 7 Understanding the Three Basic Structures (continued) Sequence structure
8. Programming Logic and Design, Fifth Edition, Comprehensive 8 Understanding the Three Basic Structures (continued) Selection structure
9. Programming Logic and Design, Fifth Edition, Comprehensive 9 Understanding the Three Basic Structures (continued) Dual-alternative if: contains two alternatives
10. Programming Logic and Design, Fifth Edition, Comprehensive 10 Understanding the Three Basic Structures (continued) Single-alternative if: contains one alternative
11. Programming Logic and Design, Fifth Edition, Comprehensive 11 Understanding the Three Basic Structures (continued) Single-alternative if
Else clause is not required
Null case: situation where nothing is done
12. Programming Logic and Design, Fifth Edition, Comprehensive 12 Understanding the Three Basic Structures (continued) Loop structure
Repeats a set of actions based on the answer to a question
Also called repetition or iteration
Question is asked first in the most common form of loop
13. Programming Logic and Design, Fifth Edition, Comprehensive 13 Understanding the Three Basic Structures (continued) Loop structure
14. Programming Logic and Design, Fifth Edition, Comprehensive 14 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
End-structure statements: indicate the end of a structure
The endif statement ends an if-then-else structure
The endwhile ends a loop structure
15. Programming Logic and Design, Fifth Edition, Comprehensive 15 Understanding the Three Basic Structures (continued)
16. Programming Logic and Design, Fifth Edition, Comprehensive 16 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: group of statements that execute as a single unit
17. Programming Logic and Design, Fifth Edition, Comprehensive 17 Understanding the Three Basic Structures (continued)
18. Programming Logic and Design, Fifth Edition, Comprehensive 18 Understanding the Three Basic Structures (continued)
19. Programming Logic and Design, Fifth Edition, Comprehensive 19 Understanding the Three Basic Structures (continued)
20. Programming Logic and Design, Fifth Edition, Comprehensive 20 Understanding the Three Basic Structures (continued) Each structure has one entry and one exit point
Structures attach to others only at entry or exit points
21. Programming Logic and Design, Fifth Edition, Comprehensive 21 Using the Priming Read Priming read (or priming input):
Reads the first input data record
Outside the loop that reads the rest of the records
Helps keep the program structured
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
22. Programming Logic and Design, Fifth Edition, Comprehensive 22 Using the Priming Read (continued) Unstructured loop
23. Programming Logic and Design, Fifth Edition, Comprehensive 23 Using the Priming Read (continued) Structured but nonfunctional loop
24. Programming Logic and Design, Fifth Edition, Comprehensive 24 Using the Priming Read (continued) Functional but nonstructured loop
25. Programming Logic and Design, Fifth Edition, Comprehensive 25 Using the Priming Read (continued) Functional and structured loop
26. Programming Logic and Design, Fifth Edition, Comprehensive 26 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
27. Programming Logic and Design, Fifth Edition, Comprehensive 27 Using the Priming Read (continued) What is wrong with this design?
28. Programming Logic and Design, Fifth Edition, Comprehensive 28 Understanding the Reasons for Structure Provides clarity
Professionalism
Efficiency
Ease of maintenance
Supports modularity
29. Programming Logic and Design, Fifth Edition, Comprehensive 29 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. Programming Logic and Design, Fifth Edition, Comprehensive 30
31. Programming Logic and Design, Fifth Edition, Comprehensive 31 Recognizing Structure (continued) Is this flowchart structured?
32. Programming Logic and Design, Fifth Edition, Comprehensive 32 Recognizing Structure (continued) Single process like A is part of an unacceptable structure
At least the beginning of a sequence structure
33. Programming Logic and Design, Fifth Edition, Comprehensive 33 Recognizing Structure (continued) B begins a selection structure
Sequences never have decisions in them
Logic never returns to B
34. Programming Logic and Design, Fifth Edition, Comprehensive 34 Recognizing Structure (continued) Pull up on the flowline from the left side of B
35. Programming Logic and Design, Fifth Edition, Comprehensive 35 Recognizing Structure (continued) Next, pull up the flowline on the right side of B
36. Programming Logic and Design, Fifth Edition, Comprehensive 36 Recognizing Structure (continued) Pull up the flowline on the left side of D and untangle it from the B selection by repeating C
37. Programming Logic and Design, Fifth Edition, Comprehensive 37 Recognizing Structure (continued) Now pull up the flowline on the right side of D
38. Programming Logic and Design, Fifth Edition, Comprehensive 38 Recognizing Structure (continued) Bring together the loose ends of D and of B
39. Programming Logic and Design, Fifth Edition, Comprehensive 39 Three Special Structures – CASE, DO-WHILE, and DO-UNTIL Many languages allow three additional structures:
The case structure
The do-while structure
The do-until structure
CASE Structure:
Decisions with more than two alternatives
Tests a variable against a series of values and takes action based on a match
Nested if-then-else statements will do what a case structure does
40. Programming Logic and Design, Fifth Edition, Comprehensive 40 Three Special Structures – Case, Do While, and Do Until (continued) Using nested if-then-else for multiple alternatives
41. Programming Logic and Design, Fifth Edition, Comprehensive 41 Three Special Structures – Case, Do While, and Do Until (continued) Using a case structure for multiple alternatives
42. Programming Logic and Design, Fifth Edition, Comprehensive 42 Three Special Structures – Case, Do While, and Do Until (continued) do-while and do-until loops
Question is asked at the end of the loop structure
Loop statements always used at least once
43. Programming Logic and Design, Fifth Edition, Comprehensive 43 Three Special Structures – Case, Do While, and Do Until (continued) do-while: executes as long as the question’s answer is Yes or True
do-until: executes as long as the question’s answer is No or False
44. Programming Logic and Design, Fifth Edition, Comprehensive 44 Three Special Structures – Case, Do While, and Do Until (continued) while loop with question at beginning is called a pretest loop
do-while and do-until with question at end are called posttest loops
Posttest loop can be replaced with a sequence followed by a pretest while loop
45. Programming Logic and Design, Fifth Edition, Comprehensive 45 Three Special Structures – Case, Do While, and Do Until (continued)
46. Programming Logic and Design, Fifth Edition, Comprehensive 46 Three Special Structures – Case, Do While, and Do Until (continued)
47. Programming Logic and Design, Fifth Edition, Comprehensive 47 Three Special Structures – Case, Do While, and Do Until (continued) How can this design be made structured?
48. Programming Logic and Design, Fifth Edition, Comprehensive 48 Three Special Structures – Case, Do While, and Do Until (continued) Repeat the needed step to enforce structure
49. Programming Logic and Design, Fifth Edition, Comprehensive 49 Summary Spaghetti code: snarled program logic
Three basic structures: sequence, selection, loop
Combined by stacking and nesting
Priming read: statement that reads the first input data record
Structured techniques promote clarity, professionalism, efficiency, and modularity
Flowchart can be made structured by untangling
50. Programming Logic and Design, Fifth Edition, Comprehensive 50 Summary (continued) case structure: questions with multiple alternatives
while loop: a pretest loop asks the question first
while loop statements never execute if the answer is No
do-while and do-until loops: posttest loops that ask the question last
do-while and do-until loop statements are always executed at least once
Posttest loop can be replaced by a sequence followed by a while loop