Write an unambiguous instruction
Quick review (2 minutes)
Ask two friends to pick out a big card from the same row, and they may pick different cards.
Each step of an algorithm has exactly one reading. The steps are carried out in the order written unless a step says where to go next. Two readers who follow the same steps on the same input do the same things and reach the same output.
Number the steps and write each one as a short instruction.
Give a name to each thing the steps use, such as the noted number, and use the same name every time.
Replace each vague word with a test a reader can carry out. Step 3 of the largest-card procedure does not say a big card. It names one card, the card taken in step 2, and one test: its number is larger than the noted number.
For each question a step asks, say what to do for yes. Say what to do for no as well, unless the reader then goes on to the next step in order, as after step 3 of the largest-card procedure. Step 2 covers both answers: go to step 5 when no card is left, and take the next card otherwise.
Say where to go after a step whenever that is not the next step in order.
Read the steps as a reader who knows nothing else would, and mark any place where two actions are possible.
The usual gap is a case nobody wrote down. Suppose step 2 said only this: take the next card. On the row 4 9 2 a reader reaches the end of the row with no instruction for what comes next. One reader stops, and another starts again from the first card. Write the instruction for the case where no card is left.
A second question sits in the word larger. If step 3 acted for a card that is larger than or equal to the noted number, the row 5 5 3 would still give 5, and the reader would rewrite the slip one more time on the way. Each wording has one reading. The step must say which of the two it means.
1 In plain words
Ask two friends to pick out a big card from the same row, and they may pick different cards. The instruction has more than one reading. An instruction is ambiguous when two readers could follow it in different ways. An instruction is unambiguous when it has exactly one reading.
A person fills the gaps in an instruction from what they know about the world. A computer follows only the precise instructions it is given, so this unit writes every step of a procedure to be unambiguous. In this unit's procedures, the same input then leads to the same actions every time.
Algorithms on paper are often written in pseudocode, which is short steps in plain words. Pseudocode has no strict rules about its commands, and it is precise enough to show what to do.
Here is the procedure this unit uses for the card problem. Call it the largest-card procedure. Step 1: take the first card from the row, write its number on a slip of paper, and call it the noted number. Step 2: if no card is left in the row, go to step 5, and otherwise take the next card. Step 3: if the card's number is larger than the noted number, cross out the noted number and write the card's number in its place. Step 4: go back to step 2. Step 5: announce the noted number and stop. The steps are carried out in the order written unless a step says where to go next. Carrying out steps in order is called sequencing.
Remember one sentence: if two readers could do different things, rewrite the step. To look it up, see section 2.1 of the Algorithms chapter of the Computer Science Field Guide.
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Programming Languages, 3.1 What's the big picture?
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.1 What's the big picture?
- College Board, AP Computer Science Principles Course and Exam Description
2 Picture it
A table of two instructions and the card each of two readers picks from the row 4 9 2. Told to pick a big card, the first reader picks 9 and the second picks 4. Told to pick the largest card, both pick 9. The row for the largest card is marked.
3 Why
An unambiguous step answers the questions a careful reader would ask. Which card comes first? Step 1 says the first card. What happens when a card equals the noted number? Step 3 acts only for a larger card, so an equal card changes nothing. When does the work end? Step 2 sends the reader to step 5 when no card is left.
Because each step has one reading, two people who follow the procedure on the same row write the same numbers on their slips and announce the same output. The Field Guide says the same of a program: it gives a specific result for any particular input.
4 The rule
Each step of an algorithm has exactly one reading.
The steps are carried out in the order written unless a step says where to go next. Two readers who follow the same steps on the same input do the same things and reach the same output.
5 How
- Number the steps and write each one as a short instruction.
- Give a name to each thing the steps use, such as the noted number, and use the same name every time.
- Replace each vague word with a test a reader can carry out. Step 3 of the largest-card procedure does not say a big card. It names one card, the card taken in step 2, and one test: its number is larger than the noted number.
- For each question a step asks, say what to do for yes. Say what to do for no as well, unless the reader then goes on to the next step in order, as after step 3 of the largest-card procedure. Step 2 covers both answers: go to step 5 when no card is left, and take the next card otherwise.
- Say where to go after a step whenever that is not the next step in order.
- Read the steps as a reader who knows nothing else would, and mark any place where two actions are possible.
6 Trace
| Step | Card taken | Noted number | Explanation |
|---|---|---|---|
| 1 | 4 | 4 | The first card shows 4. Write 4 on the slip. |
| 2 | 9 | 4 | The next card shows 9. |
| 3 | — | 9 | The card shows 9, and 9 is larger than 4. Cross out 4 and write 9. |
| 4 | — | 9 | Go back to step 2. |
| 2 | 2 | 9 | The next card shows 2. |
| 3 | — | 9 | The card shows 2, and 2 is not larger than 9. The slip stays as it is. |
| 4 | — | 9 | Go back to step 2. |
| 2 | none | 9 | No card is left. Go to step 5. |
| 5 | — | 9 | Announce 9 and stop. |
7 The trap
The usual gap is a case nobody wrote down. Suppose step 2 said only this: take the next card. On the row 4 9 2 a reader reaches the end of the row with no instruction for what comes next. One reader stops, and another starts again from the first card. Write the instruction for the case where no card is left.
A second question sits in the word larger. If step 3 acted for a card that is larger than or equal to the noted number, the row 5 5 3 would still give 5, and the reader would rewrite the slip one more time on the way. Each wording has one reading. The step must say which of the two it means.
Quick checks
Quick check 1 of 2
A row holds the cards 3 8 3. A step says: cross out the smallest card. Why is that step ambiguous for this row? Which rewrite has exactly one reading: (a) cross out a card that shows the smallest number, or (b) cross out the leftmost card that shows the smallest number?
Show the steps and the answer
Two cards show 3, so two readers could cross out different cards. Rewrite (b) has exactly one reading.
Why: Rewrite (a) still lets a reader choose either 3. The word leftmost picks out one card, the first in the row.
Quick check 2 of 2
Two steps are written in this order. Step 1: add 2 to the noted number. Step 2: double the noted number. The noted number starts at 3. What is it after both steps? What would it be if the two steps were swapped?
Show the steps and the answer
In the order written it is 10. With the steps swapped it is 8.
Why: 3 + 2 is 5, and 5 doubled is 10. Swapped, 3 doubled is 6, and 6 + 2 is 8. Sequencing carries out the steps in the order written, so the order changes the result.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- Computer Science Field Guide: Programming Languages, 3.1 What's the big picture?. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 1 Dec 2026.
- Computer Science Field Guide: Algorithms, 2.1 What's the big picture?. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 1 Dec 2026.
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 1 Dec 2026.