A Beginner's Guide To Programming Logic, Introductory: Understanding Structure
A Beginner's Guide To Programming Logic, Introductory: Understanding Structure
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
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
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