Video lecture
Watch on YouTube(opens in a new tab)Sub topic 5
Writing Steps in Plain Language
This chapter assumes you can read simple Python and know that a % b is the remainder when a is divided by b. It also assumes you have met the five properties of an algorithm and know how to write a specification with inputs, outputs, preconditions and postconditions. Here you will turn a specification into a set of plain-language steps that another person could follow exactly.
A plain-language description is usually the first written form an algorithm takes. It comes before pseudocode, before flowcharts and before code. It is also the form most easily misread, because everyday English allows vagueness that a machine, or a careful colleague, cannot resolve. This chapter shows a working format for these descriptions and the errors that break them.
The working model: structured natural language
Structured natural language means ordinary sentences arranged under a few fixed rules. The sentences stay readable to anyone. The rules make the description definite, so two people following it on the same input always do the same thing.
Here is the running example for this tutorial, Euclid's algorithm for the greatest common divisor (GCD):
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, and each part of it does a specific job.
| Part | What it does in the example | Property it supports |
|---|---|---|
| Name line | "Euclid's algorithm" identifies what is described | Readability |
| Input line | States the type, range and the "not both zero" precondition | Input |
| Output line | States what the answer means, not just its type | Output |
| Numbered steps | Give each action a fixed position and a label to jump to | Definiteness |
| Named quantities | `a`, `b`, `r` are used consistently, never "it" or "the number" | Definiteness |
| Explicit stop | Step 1 says "Stop" and names the answer | Finiteness, Output |
| Explicit jump | Step 4 names its target step | Definiteness |
| Elementary actions | Compare, take a remainder, copy values | Effectiveness |
The rules
- Open with a header that names the algorithm and copies its inputs and outputs from the specification. Steps that do not mention their inputs leave the reader guessing about the starting state.
- Number every step. A number lets other steps point to it ("go back to step 1") and lets a reader report a problem precisely ("step 3 is ambiguous").
- Write one action per step, or one decision with its consequences. If a step contains "and then", consider splitting it.
- Name every quantity the first time it appears, and use that exact name afterwards. "Compute r, the remainder of a divided by b" introduces
rand defines it in the same sentence. - Make control flow explicit. Every step either continues to the next step, jumps to a named step, or stops. Nothing is left as "and so on".
- State the answer at every stopping point. "Stop" with no answer leaves the output undefined.
- Use only actions the reader can carry out mechanically. "Choose a suitable value" requires judgment. "Choose the first element" does not.
How steps connect
Plain-language steps form a small control graph. Most steps fall through to the next number. Decisions branch, and "go back" steps create loops. Drawing that graph for Euclid's algorithm shows that every path ends at the single stop in step 1.
If you cannot draw this graph from your description, because a step has no clear successor or a jump target is missing, the description is not yet definite. A later chapter covers flowcharts as a notation in their own right. Here the drawing is only a check.
Common errors and how to fix them
Most broken plain-language descriptions fail in a handful of predictable ways.
Ambiguous operations
"Divide a by b" could mean real division (48 / 18 = 2.67), integer division (2), or the remainder (12). Euclid's algorithm needs the remainder, so the description says "the remainder of a divided by b". When an English verb has several mathematical meanings, name the one you mean.
For negative numbers there is a further ambiguity: the remainder of −7 divided by 3 is −1 in some conventions and 2 in others. The precondition "non-negative integers" removes this case. That is one reason the input line matters to how the steps are written.
Order-dependent updates
Step 3 says "Replace a with b, and b with r." This works because r was saved in step 2. A tempting shortcut merges the steps: "Replace a with b, and b with the remainder of a divided by b." Read literally and in order, this computes the remainder using the new a, which is already equal to b. The remainder of a number divided by itself is 0, so the algorithm stops after one round with the wrong answer.
The Python below runs both readings so you can see the difference.
def gcd_correct(a, b):
while b != 0:
r = a % b # step 2: save the remainder first
a = b # step 3: then replace a ...
b = r # ... and b
return a
def gcd_sequential_bug(a, b):
while b != 0:
a = b # "replace a with b"
b = a % b # "and b with a mod b" -- uses the NEW a, so always 0
return a
print(gcd_correct(48, 18)) # 6
print(gcd_sequential_bug(48, 18)) # 18, which is wrongThe fix in plain language is to introduce a named temporary (r) or to say "at the same time" explicitly: "Simultaneously set a to b and b to the old a's remainder when divided by the old b." The temporary is usually clearer.
Pronouns and unnamed quantities
"Compare it with the largest so far and keep it if it is bigger" contains three uses of "it" and an unnamed "largest so far". Rewrite with names: "Compare x with best. If x is greater than best, set best to x."
Vague loops
"Repeat until done" and "keep going through the list" do not say what "done" means or what moves forward on each pass. A definite loop names its exit test and its step. For example: "If there are no more elements, go to step 6. Otherwise, let x be the next element and go to step 4."
Missing stops and missing answers
A description that ends at its last numbered step without saying "Stop" and naming the output relies on the reader to infer the ending. Some descriptions also have a second exit, such as "not found", that is never written down. Every exit needs its own "Stop" and its own answer.
Unhandled edge cases
Check the boundaries of the precondition against the steps. For Euclid's algorithm:
| Input | What the steps do | Result |
|---|---|---|
| a = 48, b = 18 | Several rounds of steps 2 to 4 | 6 |
| a = 18, b = 48 | First round swaps them: a = 48, b = 18 | 6 |
| a = 7, b = 0 | Step 1 stops immediately | 7 |
| a = 0, b = 5 | One round gives a = 5, b = 0 | 5 |
| a = 0, b = 0 | Excluded by the precondition | Undefined, since gcd(0, 0) has no single agreed value |
The second row is worth noticing. The description does not need a separate "if a < b, swap" step, because the remainder of a smaller number divided by a larger one is the smaller number itself, and step 3 then swaps the two values. Testing edge cases shows you which steps are needed and which are not.
Steps that require judgment
"Pick a good starting guess" or "simplify the expression" is not effective, because it cannot be carried out mechanically. Either give the rule ("start with guess = n / 2") or mark the step as a call to another, separately described algorithm ("sort the list using any correct sorting algorithm").
A second example: finding the maximum
Applying the rules from scratch to a new problem shows how they work together. The specification: given a non-empty list of comparable values, return the largest.
Find maximum (plain language)
Input: a non-empty list xs of values that can be compared with "greater than".
Output: the largest value in xs.
1. Let best be the first element of xs.
2. Let i be 2 (the position of the second element).
3. If i is greater than the length of xs, the answer is best. Stop.
4. Let x be the element of xs at position i.
5. If x is greater than best, set best to x.
6. Increase i by 1.
7. Go back to step 3.Every quantity (best, i, x) is named before use. The loop has an explicit exit test (step 3), an explicit advance (step 6) and an explicit jump (step 7). Positions are counted from 1, and the description says so implicitly by calling position 2 "the second element". If you intend to hand this to a programmer working in a language that counts from 0, state the counting convention in the header.
The precondition "non-empty" is what makes step 1 legal. Remove it, and step 1 refers to an element that may not exist.
Exercises
Exercise
A colleague writes: "1. Look through the numbers. 2. Remember the smallest one. 3. Output it." List at least three problems with this description, using the rules from this chapter.
Show answer
It has no header stating the input (what kind of numbers, and whether the list can be empty) or the meaning of the output. Step 1 is a vague loop with no named element, no exit test and no advance. "The smallest one" in step 2 is never named, and the description never says how to compare or when to update it. "It" in step 3 is a pronoun with no clear referent. There is no explicit "Stop", and an empty list is not handled.
Exercise
Rewrite Euclid's steps 2 and 3 as a single step that is still correct when read literally from left to right.
Show answer
One correct version is: "2. Let r be the remainder of a divided by b; then set a to b; then set b to r." The temporary r must be computed before a changes. Another correct version is: "2. At the same time, set a to the old b and b to the remainder of the old a divided by the old b." The key point is that the remainder must use the values of a and b from before the update.
Exercise
Write a structured plain-language description of linear search. Input: a list items and a value target. Output: the position of the first element equal to target, or -1 if there is none. Make sure every exit states its answer.
Show answer
Header: "Input: a list items (possibly empty) and a value target. Output: the position (counting from 0) of the first element equal to target, or -1 if no element equals target." Steps: "1. Let i be 0. 2. If i equals the length of items, the answer is -1. Stop. 3. If the element of items at position i equals target, the answer is i. Stop. 4. Increase i by 1. 5. Go back to step 2." Both exits (steps 2 and 3) name their answers, and the empty list is handled by step 2 on the first pass.