0% found this document useful (0 votes)
60 views

A Beginner's Guide To Programming Logic, Introductory: Understanding Structure

The document discusses structured and unstructured programming. Spaghetti code is unstructured with tangled logic that is difficult to read and maintain. Programs can be structured using three basic structures: sequence, selection, and loop. Sequence performs steps in order, selection uses if/else logic to choose between options, and loops repeat steps. Well-structured programs use these structures and nesting/stacking them to clearly organize program logic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

A Beginner's Guide To Programming Logic, Introductory: Understanding Structure

The document discusses structured and unstructured programming. Spaghetti code is unstructured with tangled logic that is difficult to read and maintain. Programs can be structured using three basic structures: sequence, selection, and loop. Sequence performs steps in order, selection uses if/else logic to choose between options, and loops repeat steps. Well-structured programs use these structures and nesting/stacking them to clearly organize program logic.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

A Beginner’s Guide to

Programming Logic,
Introductory

Chapter 3
Understanding Structure
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 2
Figure 3-1 Spaghetti code logic for washing a dog
A Beginner's Guide to Programming Logic, Introductory 3
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 4


Understanding the Three Basic
Structures (continued)

Figure 3-2 Sequence structure

A Beginner's Guide to Programming Logic, Introductory 5


Understanding the Three Basic
Structures (continued)

Figure 3-3 Selection structure

A Beginner's Guide to Programming Logic, Introductory 6


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

A Beginner's Guide to Programming Logic, Introductory 7


Understanding the Three Basic
Structures (continued)
• Single-alternative if
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
– Else clause is not required
• null case
– Situation where nothing is done

A Beginner's Guide to Programming Logic, Introductory 8


Understanding the Three Basic
Structures (continued)

Figure 3-4 Single-alternative selection structure

A Beginner's Guide to Programming Logic, Introductory 9


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

A Beginner's Guide to Programming Logic, Introductory 10


Understanding the Three Basic
Structures (continued)

Figure 3-5 Loop structure

A Beginner's Guide to Programming Logic, Introductory 11


Understanding the Three Basic
Structures (continued)
• Loop structure

while testCondition continues to be true


do someProcess

while quantityInInventory remains low


continue to orderItems

A Beginner's Guide to Programming Logic, Introductory 12


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 13
Understanding the Three Basic
Structures (continued)

Figure 3-6 Structured flowchart and pseudocode with three stacked structures
A Beginner's Guide to Programming Logic, Introductory 14
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 15


Understanding the Three Basic
Structures (continued)

Figure 3-7 Flowchart and pseudocode showing nested structures—a


sequence nested within a selection

A Beginner's Guide to Programming Logic, Introductory 16


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

A Beginner's Guide to Programming Logic, Introductory 17


Understanding the Three Basic
Structures (continued)

Figure 3-9 Flowchart and pseudocode for loop within selection within
sequence within selection
A Beginner's Guide to Programming Logic, Introductory 18
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

A Beginner's Guide to Programming Logic, Introductory 19


Using a Priming Input to Structure
a 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 20
Using a Priming Input to Structure
a Program (continued)

Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling


problem
A Beginner's Guide to Programming Logic, Introductory 21
Using a Priming Input to Structure
a Program (continued)

Figure 3-16 Functional but unstructured flowchart


A Beginner's Guide to Programming Logic, Introductory 22
Using a Priming Input to Structure
a 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
start
get inputNumber
while not eof
calculatedAnswer = inputNumber * 2
print calculatedAnswer
get inputNumber
endwhile
stop
A Beginner's Guide to Programming Logic, Introductory 23
Figure 3-17 Functional, structured flowchart and pseudocode for the number-
doubling problem

A Beginner's Guide to Programming Logic, Introductory 24


Figure 3-18 Structured but incorrect solution to the number-doubling problem

A Beginner's Guide to Programming Logic, Introductory 25


Understanding the Reasons for
Structure
• Clarity
• Professionalism
• Efficiency
• Ease of maintenance
• Supports modularity

A Beginner's Guide to Programming Logic, Introductory 26

You might also like