logic formulation 3
DESCRIPTION
A Beginner’s Guide to Programming Logic, Introductory Chapter 3 Understanding Structure Objectives In this chapter, you will learn about: - The features of unstructured spaghetti code - The three basic structures—sequence, selection, and loop - Using a priming input to structure a program - The need for structure - Recognizing structure - Structuring and modularizing unstructured logic COURSE TECHNOLOGY CENGAGE LearningTRANSCRIPT
![Page 1: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/1.jpg)
A Beginner’s Guide to Programming Logic,
Introductory
Chapter 3
Understanding Structure
![Page 2: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/2.jpg)
Objectives
In this chapter, you will learn about:
• The features of unstructured spaghetti code
• The three basic structures—sequence, selection, and loop
• Using a priming input to structure a program
• The need for structure
• Recognizing structure
• Structuring and modularizing unstructured logic
A Beginner's Guide to Programming Logic, Introductory 2
![Page 3: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/3.jpg)
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
A Beginner's Guide to Programming Logic, Introductory 3
![Page 4: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/4.jpg)
A Beginner's Guide to Programming Logic, Introductory 4
Figure 3-1 Spaghetti code logic for washing a dog
![Page 5: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/5.jpg)
Understanding the Three Basic Structures
• Structure– Basic unit of programming logic – 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 ifs
– Loop• Repeat actions based on answer to a question
A Beginner's Guide to Programming Logic, Introductory 5
![Page 6: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/6.jpg)
A Beginner's Guide to Programming Logic, Introductory 6
Understanding the Three Basic Structures (continued)
Figure 3-2 Sequence structure
![Page 7: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/7.jpg)
A Beginner's Guide to Programming Logic, Introductory 7
Understanding the Three Basic Structures (continued)
Figure 3-3 Selection structure
![Page 8: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/8.jpg)
A Beginner's Guide to Programming Logic, Introductory 8
Understanding the Three Basic Structures (continued)
• Dual-alternative if– Contains two alternatives– If-then-else structure
if someCondition is true then
do oneProcess
else
do theOtherProcess
![Page 9: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/9.jpg)
Understanding the Three Basic Structures (continued)
• Single-alternative if
– Else clause is not required
• null case– Situation where nothing is done
A Beginner's Guide to Programming Logic, Introductory 9
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
![Page 10: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/10.jpg)
A Beginner's Guide to Programming Logic, Introductory 10
Understanding the Three Basic Structures (continued)
Figure 3-4 Single-alternative selection structure
![Page 11: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/11.jpg)
A Beginner's Guide to Programming Logic, Introductory 11
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
loop– while … do or while loop
![Page 12: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/12.jpg)
A Beginner's Guide to Programming Logic, Introductory 12
Understanding the Three Basic Structures (continued)
Figure 3-5 Loop structure
![Page 13: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/13.jpg)
A Beginner's Guide to Programming Logic, Introductory 13
Understanding the Three Basic Structures (continued)
• Loop structure
while testCondition continues to be true
do someProcess
while quantityInInventory remains low
continue to orderItems
![Page 14: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/14.jpg)
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
A Beginner's Guide to Programming Logic, Introductory 14
![Page 15: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/15.jpg)
A Beginner's Guide to Programming Logic, Introductory 15
Understanding the Three Basic Structures (continued)
Figure 3-6 Structured flowchart and pseudocode with three stacked structures
![Page 16: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/16.jpg)
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
A Beginner's Guide to Programming Logic, Introductory 16
![Page 17: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/17.jpg)
A Beginner's Guide to Programming Logic, Introductory 17
Understanding the Three Basic Structures (continued)
Figure 3-7 Flowchart and pseudocode showing nested structures—asequence nested within a selection
![Page 18: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/18.jpg)
A Beginner's Guide to Programming Logic, Introductory 18
Understanding the Three Basic Structures (continued)
Figure 3-8 Flowchart and pseudocode showing nested structures—a loop nested within a sequence, nested within a selection
![Page 19: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/19.jpg)
A Beginner's Guide to Programming Logic, Introductory 19
Understanding the Three Basic Structures (continued)
Figure 3-9 Flowchart and pseudocode for loop within selection within sequence within selection
![Page 20: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/20.jpg)
A Beginner's Guide to Programming Logic, Introductory 20
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– Any structure can be nested within another structure
![Page 21: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/21.jpg)
Using a Priming Input to Structurea Program
• 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
A Beginner's Guide to Programming Logic, Introductory 21
![Page 22: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/22.jpg)
Using a Priming Input to Structurea Program (continued)
A Beginner's Guide to Programming Logic, Introductory 22
Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling problem
![Page 23: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/23.jpg)
Using a Priming Input to Structurea Program (continued)
A Beginner's Guide to Programming Logic, Introductory 23
Figure 3-16 Functional but unstructured flowchart
![Page 24: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/24.jpg)
A Beginner's Guide to Programming Logic, Introductory 24
Using a Priming Input to Structurea 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
startget inputNumberwhile not eof
calculatedAnswer = inputNumber * 2print calculatedAnswerget inputNumber
endwhilestop
![Page 25: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/25.jpg)
A Beginner's Guide to Programming Logic, Introductory 25
Figure 3-17 Functional, structured flowchart and pseudocode for the number-doubling problem
![Page 26: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/26.jpg)
A Beginner's Guide to Programming Logic, Introductory 26
Figure 3-18 Structured but incorrect solution to the number-doubling problem
![Page 27: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/27.jpg)
Understanding the Reasons for Structure
• Clarity
• Professionalism
• Efficiency
• Ease of maintenance
• Supports modularity
A Beginner's Guide to Programming Logic, Introductory 27
![Page 28: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/28.jpg)
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
A Beginner's Guide to Programming Logic, Introductory 28
![Page 29: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/29.jpg)
A Beginner's Guide to Programming Logic, Introductory 29
Recognizing Structure (continued)
Figure 3-20 Example 2
![Page 30: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/30.jpg)
A Beginner's Guide to Programming Logic, Introductory 30
Recognizing Structure (continued)
Figure 3-21 Example 3
![Page 31: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/31.jpg)
A Beginner's Guide to Programming Logic, Introductory 31
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 32: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/32.jpg)
A Beginner's Guide to Programming Logic, Introductory 32
• 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 33: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/33.jpg)
A Beginner's Guide to Programming Logic, Introductory 33
• Pull up on the flowline from the left side of H
Figure 3-24 Untangling Example 3, third step
Recognizing Structure (continued)
![Page 34: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/34.jpg)
A Beginner's Guide to Programming Logic, Introductory 34
• Next, pull up the flowline on the right side of H
Figure 3-25 Untangling Example 3, fourth step
Recognizing Structure (continued)
![Page 35: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/35.jpg)
A Beginner's Guide to Programming Logic, Introductory 35
• 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 36: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/36.jpg)
A Beginner's Guide to Programming Logic, Introductory 36
• Now pull up the flowline on the right side of I
Figure 3-27 Untangling Example 3, sixth step
Recognizing Structure (continued)
![Page 37: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/37.jpg)
A Beginner's Guide to Programming Logic, Introductory 37
• 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 38: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/38.jpg)
Structuring and ModularizingUnstructured Logic
• Dog-washing process– Unstructured– Can be reconfigured to be structured
• First step simple sequence
A Beginner's Guide to Programming Logic, Introductory 38
Figure 3-29 Washing the dog, part 1
![Page 39: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/39.jpg)
Structuring and ModularizingUnstructured Logic (continued)
• After the dog runs away– Catch the dog and determine whether he runs away
again– A loop begins
A Beginner's Guide to Programming Logic, Introductory 39
Figure 3-30 Washing the dog, part 2
![Page 40: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/40.jpg)
A Beginner's Guide to Programming Logic, Introductory 40
Figure 3-31 Washing the dog, part 3
![Page 41: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/41.jpg)
A Beginner's Guide to Programming Logic, Introductory 41
Figure 3-33 Structured dog-washing flowchart and pseudocode
![Page 42: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/42.jpg)
A Beginner's Guide to Programming Logic, Introductory 42
Figure 3-34 Modularized version of the dog-washing program
![Page 43: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/43.jpg)
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
A Beginner's Guide to Programming Logic, Introductory 43
![Page 44: Logic Formulation 3](https://reader035.vdocument.in/reader035/viewer/2022062220/558bcfe0d8b42ad1448b4576/html5/thumbnails/44.jpg)
Summary (continued)
• Structured techniques promote: – Clarity– Professionalism– Efficiency– Modularity
• Flowchart can be made structured by untangling
A Beginner's Guide to Programming Logic, Introductory 44