# Structured Program Design

Computer Science I · Programming II · https://tryals.app/learn/computer-science-i/structured-program-design

## Building Well-Structured Programs

Once code works, the next skill is keeping it understandable as it grows. **Structured programming** builds programs using only three fundamental control structures:

| Structure | Definition |
| --- | --- |
| **Sequence** | Statements run in order |
| **Selection** | Choosing a path with if/else |
| **Iteration** | Repeating with loops |

The **structured program theorem** proves these three suffice for any computation. Unrestricted jumps produce **spaghetti code**, an unmaintainable tangle. Structured programming ensures every block has one entrance and one exit.

> **Pitfall:** Believing working code needs no structure. Structure is for future human understanding, not today's output.

## Top-Down Design & Modularity

Good structure relies on **top-down decomposition**: breaking a large task into smaller sub-tasks or functions until each piece is simple. Two design virtues guide this process:

| Virtue | Description |
| --- | --- |
| **High Cohesion** | Each unit does one well-defined thing |
| **Low Coupling** | Units depend on each other as little as possible |

Together, **high cohesion** and **low coupling** let you change one part of a program without breaking others. Always measure code quality by human readability, single-entry blocks, and modularity.

## Practice questions

4 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Select every statement that is a consequence of the structured program theorem as the lesson states it.

A. A program built from the three constructs is guaranteed to be free of bugs
B. Any computation can be expressed using only sequence, selection and iteration
C. Arbitrary jumps are never necessary, however complicated the task
D. A goto-free program can still be badly designed
E. A structured program always runs faster than an unstructured one

**Answer:** B. Any computation can be expressed using only sequence, selection and iteration; C. Arbitrary jumps are never necessary, however complicated the task; D. A goto-free program can still be badly designed

**Why:** The theorem says the three constructs are sufficient, so jumps are never needed, but sufficiency says nothing about correctness or speed, and a goto-free program can still have terrible cohesion and coupling. Reading 'structured' as 'good' is the pitfall: structure is measured by cohesion, coupling and single-entry blocks, not by the absence of one keyword.

Page: https://tryals.app/practice/computer-science-i/structured-program-design/select-every-statement-that-is-a-consequence-of-the-structured

### 2. Dijkstra's *"Go To Statement Considered Harmful"* argued about human limits, not machine ones. What exactly was the argument?

A. That goto compiles to slower machine code
B. That goto produces incorrect programs more often than structured constructs
C. That goto was needed only because early compilers lacked loop constructs
D. That unrestricted jumps make it impossible to reason locally

**Answer:** D. That unrestricted jumps make it impossible to reason locally

**Why:** With arbitrary jumps, understanding a line means finding every place that could jump to it, the whole program becomes the context. Structured blocks are entered at the top and left at the bottom, so a block can be reasoned about alone. The constraint is about human working memory, which is why the argument still applies to code no machine finds difficult.

Page: https://tryals.app/practice/computer-science-i/structured-program-design/dijkstras-go-to-statement-considered-harmful-argued-about-human

### 3. Order the steps of top-down decomposition as the lesson describes it.

**Answer:**

1. State the whole task in one sentence
2. Break the task into a few major sub-tasks
3. Give each sub-task a clear name and a defined job
4. Refine any sub-task that is still too complicated into smaller ones
5. Stop when every remaining piece is simple enough to write directly

**Why:** Decomposition starts from the whole and repeatedly splits, naming each piece as it goes, until nothing left is complicated. Starting from small pieces and gluing them together is bottom-up, which is a different discipline; the top-down order is what keeps each level a complete description of the level above.

Page: https://tryals.app/practice/computer-science-i/structured-program-design/order-the-steps-of-top-down-decomposition-as-the-lesson-describes-it

### 4. Structured programming allows only sequence, selection and iteration, and it was proved that these three suffice for any computable function. Why did anyone need to prove that, and what was being given up?

A. Because the proof showed that goto produces incorrect results, unlike the three constructs.
B. Because goto is impossible to implement on modern hardware, so a replacement was needed.
C. Because the three constructs are faster to execute, and the proof established that no speed was lost.
D. Because the alternative was goto, and removing it costs no expressive power.

**Answer:** D. Because the alternative was goto, and removing it costs no expressive power.

**Why:** Böhm–Jacopini mattered because it removed the obvious objection: programmers could reasonably ask whether dropping goto made some programs unwriteable, and the theorem answered no. What is given up is arbitrary jumps, and that is the point, control enters a block at the top and leaves at the bottom, so a block can be understood alone. The argument was about human reasoning, not machine behaviour.

Page: https://tryals.app/practice/computer-science-i/structured-program-design/structured-programming-allows-only-sequence-selection-and-iteration
