Video lecture
Watch on YouTube(opens in a new tab)Sub topic 5
Writing Steps in Plain Language
This chapter assumes you can read basic code with variables, conditionals and loops, and that you know what the remainder (modulo) of an integer division is. It also builds on the previous chapter's idea of a problem specification, meaning the stated inputs, outputs, preconditions and postconditions.
Before an algorithm becomes pseudocode or a program, it usually starts as a description in ordinary language. A good plain-language description is easier to write than code and can be read by anyone. It is still precise enough that two readers would carry out the same actions.
Why plain language needs structure
Everyday instructions tolerate vagueness. "Stir until it looks right" works in a kitchen because the cook fills in the gaps. An algorithm cannot leave gaps, because its properties require each step to be definite and effective. Structured natural language keeps the readability of prose and adds a few rules that remove ambiguity.
The running example
Here is Euclid's algorithm for the greatest common divisor, written in structured plain language:
Euclid's algorithm (plain language)
Input: two non-negative integers a and b, not both zero.
Output: the greatest common divisor of a and b.
1. If b is 0, the answer is a. Stop.
2. Otherwise, compute r, the remainder of a divided by b.
3. Replace a with b, and b with r.
4. Go back to step 1.The description is short, but every line does a job.
Conventions that make it work
| Convention | Example from Euclid | What it prevents |
|---|---|---|
| State input and output first | "Input: two non-negative integers..." | Guessing what data the steps act on |
| Number the steps | 1, 2, 3, 4 | Ambiguity about order and jump targets |
| Name the values | a, b, r | Vague references such as "the number" or "it" |
| One action per step | Step 2 only computes r | Steps that hide several decisions |
| Make stopping explicit | "Stop." in step 1 | A description that never says when it ends |
| Jump by step number | "Go back to step 1" | Unclear repetition such as "keep doing this" |
The Input and Output lines repeat the specification, so a reader can check the steps against it without looking elsewhere.
Common errors
- Undefined terms. Writing "reduce the pair" without saying how. Spell out the operation.
- Hidden state. Saying "use the old value" after it has been replaced. In step 3, the order of a and b matters. Write "Replace a with b, and b with r" so the reader knows that the old b becomes the new a.
- Missing cases. Forgetting what happens when b is already 0. Step 1 handles it before any division, which also avoids dividing by zero.
- Implicit loops. "Repeat as needed" does not say what to repeat or when to stop. Name the step to return to and the condition that ends the loop.
- Mixing levels. Putting one very detailed step next to one very vague step. Keep the steps at a similar grain.
How the description flows
Notice that the numbered steps already contain a decision (step 1) and a repetition (step 4). Later chapters name these building blocks and turn the same description into pseudocode.
Exercise
Rewrite this vague description as structured plain language: "Go through the list and find the biggest number." Include Input and Output lines, numbered steps, and an explicit stop.
Show answer
Input: a non-empty list xs of numbers. Output: the largest number in xs. 1. Set best to the first element of xs. 2. If there are no more elements to look at, the answer is best. Stop. 3. Otherwise, let x be the next element. 4. If x is greater than best, replace best with x. 5. Go back to step 2.
Exercise
A student writes step 3 of Euclid as "Replace b with r, and a with b." Why is this wrong?
Show answer
If b is replaced first, then "a with b" copies the new value r into a, and the original b is lost. The step must say that a receives the old value of b, or it must state the order of the replacements explicitly.