A Quick Intro to Structured Programming Notes

Origins and Context of Structured Programming

  • Source Material: This guide is based on a presentation by Chris Wells for the course CSCI 1103.

  • Theoretical Influence: The content is inspired in part by the textbook "Just Enough Programming Logic and Design" by Joyce Farrell.

  • Objective: To introduce the fundamental principles of structured programming used to create manageable, reliable software.

The Problem of Spaghetti Code

  • Definition: "Spaghetti code" refers to unstructured program code that is complex and disorganized.

  • Visualization: When mapped out using a flowchart, spaghetti code resembles a tangled ball of spaghetti, with lines of logic crossing and looping in a confusing manner.

  • Inherent Flaws:

    • Fragility: The code is prone to breaking when small changes are made.

    • Maintenance Issues: It is difficult, and sometimes nearly impossible, to follow the logical flow, making it extremely hard to update or debug.

  • The Role of GOTO Statements:

    • The use of the GOTOGOTO statement in certain programming languages is a primary cause of spaghetti code, as it allows the program to jump to any point in the code indiscriminately.

    • Modern Avoidance: Modern programming environments and languages are designed to prevent this; for example, the GOTOGOTO statement does not exist in Flowgorithm or Python.

  • The Solution: Students and developers can avoid spaghetti code entirely by building programs using only three basic logic structures.

The Sequence Structure

  • Definition: A sequence executes a series of tasks one after the other in a linear fashion.

  • Behavior:

    • Tasks are performed one at a time.

    • A sequence can contain any number of individual tasks.

  • Flowchart representation logic: In a standard sequence, the program flow moves directly from the start to the end through various operations including:

    • Main\text{Main} (Initialization/Start)

    • Declare\text{Declare} (Variable definition)

    • Input\text{Input} (Data entry)

    • Assign\text{Assign} (Calculation or value assignment)

    • Output\text{Output} (Display or data exportation)

    • End\text{End} (Termination)

The Selection Structure

  • Alternative Names: Often referred to as an if statement\text{if statement} or an if-then-else statement\text{if-then-else statement}.

  • Core Function: This structure evaluates a condition to determine which path the program should take.

  • Single-Alternative If Structure:

    • If the condition is true, a specific task is performed.

    • If the condition is false, the task is skipped entirely, and the program proceeds directly to the next structure in the sequence.

  • Dual-Alternative If Structure:

    • If the condition is true, one specific task is performed.

    • If the condition is false, a different, specific task is performed instead.

    • In both cases, the paths eventually rejoin to continue the program flow.

The Loop Structure

  • Definition: A loop repeats a task or a set of tasks based on a specific logic condition.

  • Behavior:

    • The program repeats the action while the condition remains true.

    • Once the condition evaluates to false, the loop terminates, and the program moves to the next task or structure in the sequence.

  • Flowchart Components:

    • While\text{While} condition check.

    • Assign\text{Assign} or task execution path for the true branch.

    • An exit path for the false branch.

Advanced Logic Management: Stacking and Nesting

  • Universal Problem Solving: The combination of sequences, selections (if statements), and loops is flexible enough to solve virtually any programming problem.

  • Stacking Structures:

    • Definition: Following one structure with another in a sequence.

    • Scalability: Structures can be stacked indefinitely.

    • Example Integration: A sequence can lead directly into a loop, which can then be followed by a selection structure.

  • Nesting Structures:

    • Definition: Placing one structure inside another structure.

    • Interdependence:

      • An if statement\text{if statement} (selection) can contain a loop within its true or false branches.

      • A loop\text{loop} can contain another logic structure, such as an if statement\text{if statement}, inside its repeating body.

    • Specific Examples Captured:

      1. A loop followed by a selection structure (Stacking).

      2. A sequence containing a selection structure that is itself nested inside a loop (Nesting).