Computer Science Foundations / Algorithms on paper
Algorithms on paper
All 5 lessons, in order.
The unit at a glance
- State inputs and outputs
A friend slides a row of number cards across the table and asks for the largest number.
- Write an unambiguous instruction
Ask two friends to pick out a big card from the same row, and they may pick different cards.
- Trace a decision
A library desk has a rule for late books, invented for this lesson.
- Explain when a repetition ends
A cashier counting a drawer of coins repeats one small action: take a coin and add it to the count.
- Check a procedure against a counterexample
A procedure can look right and be wrong.
Rules (5)
- State inputs and outputs
A problem statement names the input, says which inputs are allowed and names the output each allowed input needs. An algorithm solves the problem when it ends with the correct output for every allowed input.
- Write an unambiguous instruction
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.
- Trace a decision
At a selection step, work out the condition with the current values each time the step is reached. If it is true, carry out the action the step gives for true. If it is false, carry out the step's otherwise action when it has one. After either, follow any instruction about where to go next, and continue with the next step in order when there is none. The condition decides which action is carried out.
- Explain when a repetition ends
A loop repeats its steps until its stopping condition is met. If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.
- Check a procedure against a counterexample
A test case is an allowed input together with the output expected for it. In this unit the expected output is worked out by hand before the procedure is carried out. A procedure passes a test when it announces that output. An allowed input for which it announces anything else is a counterexample, and one counterexample shows that the procedure does not solve the problem. Passing tests cover only the inputs tried.
Recipes (5)
- State inputs and outputs
- Write the word Input and say what the algorithm is given. For the card problem, that is a row of cards.
- Add the limits on the input: how many items, and of what kind. Here the limits are one or more cards, each showing an integer.
- Write the word Output and say what must be handed back. Here it is the largest number in the row.
- Choose three or four allowed inputs. Include the smallest allowed input. For the card problem, add a row with a repeated number as well.
- Work out the correct output for each chosen input by hand and write it beside the input. Keep the table for testing.
- Write an unambiguous instruction
- 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.
- Trace a decision
- Draw a table with a column for the step, a column for the condition with its values filled in and a column for what the procedure holds after the step.
- Write the input above the table.
- Carry out the steps in order. At a selection step, copy the condition with the real values in place of the names.
- Write true or false beside the condition. If it is true, do the action the step gives for true. If it is false, do the step's otherwise action when it has one. After either, follow any instruction about where to go next, and move to the next step in order when there is none.
- Stop at the step that says stop. The output is what that step announces.
- Trace again with inputs that take the other paths: one input that makes each selection step's action run, and one for which no condition is true. For the late-fee procedure,
0,3and8days late do that.
- Explain when a repetition ends
- Find the steps that repeat, and mark the step that sends the reader back.
- Find the stopping condition, which is the test that lets the reader leave the loop.
- Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.
- Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.
- Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.
- Write one sentence that names the stopping condition and the step that moves toward it.
- Check a procedure against a counterexample
- Take the table of inputs and correct outputs made with the problem statement, as in lesson 1.
- Add inputs at the extremes of what is allowed, such as the smallest allowed row. Add unusual rows too: a row with a repeated value, and a row whose numbers are all below zero.
- Trace the procedure on each input, and write what it announces beside the expected output.
- Mark each row pass or fail. A failed row is a counterexample.
- For a counterexample, identify the error and correct it. One way to find it is to look for the first step where the trace differs from a correct run. Correct that step and any later step that depends on it.
- Carry out every test again after the correction, including the tests that passed before.
Traps (5)
- State inputs and outputs
The common slip is writing steps before the limits are settled. A statement that says a row of cards and stops there leaves the empty row open. One reader decides the output is
0, and another decides there is none. Decide which inputs are allowed and write the decision into the statement. In this unit the empty row is not allowed.A second slip is naming the wrong output. The largest number in
5 5 3is5. The position of the largest card is a different output, and for this row two positions hold it. - Write an unambiguous instruction
The usual gap is a case nobody wrote down. Suppose step 2 said only this: take the next card. On the row
4 9 2a 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 3would still give5, 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. - Trace a decision
Do not skip a step because the answer seems clear. Carry out every step the procedure reaches, including the ones whose condition is false.
The opposite slip is carrying out an action whose condition was false, or going on to step 3 after step 2 has said go to step 5. For
0days late that slip announces1coin where the rule gives0. Sequencing means carrying out the steps in the order the procedure gives them. Follow each jump as written. - Explain when a repetition ends
A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.
The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes
1, then repeats two steps: add2to the number, and stop if the number is10. The number goes3,5,7,9,11and on, and never equals10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes. - Check a procedure against a counterexample
After several passing tests it is tempting to call the procedure correct. The three rows that pass all hold a number above
0, which hides the mistake. The changed procedure fails only on rows whose numbers are all below0. Choose tests that differ from each other in kind, including inputs at the extremes of what is allowed.A second slip is patching the counterexample alone, for instance with a new step that announces
-3for that one row. The row-9 -4would then fail in the same way. Correct the steps that cause the error by restoring the first two steps of the largest-card procedure of lesson 2. Step 1 takes the first card and writes its number on the slip, and step 2 goes on with the cards that are left.
Lesson 1 of 5
State inputs and outputs
Open this lessonLeans on 1Comes back in 0
Leans on
Comes back in
No lesson links recorded.
1 In plain words
A friend slides a row of number cards across the table and asks for the largest number. Before you plan a single step, settle two things: what you are given, and what you must hand back.
An algorithm is a finite set of instructions that carries out a specific task. The Computer Science Field Guide adds that an algorithm gives the correct result every time.
A program's input is data sent to it for processing, and its output is data it sends out. For an algorithm on paper, the input is what you are given at the start, and the output is what you hand back at the end. A problem statement names both and says which inputs are allowed.
The main problem of this unit is invented for these lessons. The input is a row of one or more cards, each showing an integer. In this unit an integer means a number with no fraction part, and it may be negative, zero or positive. The output is the largest number in the row. For the row 4 9 2 the output is 9. Working out the largest of several numbers is a task with well-known algorithms.
Remember one sentence: say what goes in and what comes out before you say how. That advice is this lesson's own. For the definition of an algorithm, find essential knowledge AAP-2.A.1 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of four input rows and their outputs. The row 4 9 2 needs the output 9. The row 7 needs 7. The row 5 5 3 needs 5. The row -3 -8 needs -3. The one-card row and the row of negative numbers are marked.
3 Why
Stating the output first gives you something to check a procedure against. For the row 4 9 2, a correct procedure must hand back 9, whatever its steps are. Testing compares what a procedure produces with outputs worked out beforehand.
The statement has to cover every allowed input. A row with one card is allowed, so the output for the row 7 is 7. A number may repeat, so the output for 5 5 3 is 5. Negative integers are allowed too. Of two negative numbers, the one closer to zero is the larger, so the output for -3 -8 is -3. A procedure that is right for some allowed rows and wrong for others does not solve the problem, because an algorithm gives the correct result every time.
4 The rule
A problem statement names the input, says which inputs are allowed and names the output each allowed input needs.
An algorithm solves the problem when it ends with the correct output for every allowed input.
5 How
- Write the word Input and say what the algorithm is given. For the card problem, that is a row of cards.
- Add the limits on the input: how many items, and of what kind. Here the limits are one or more cards, each showing an integer.
- Write the word Output and say what must be handed back. Here it is the largest number in the row.
- Choose three or four allowed inputs. Include the smallest allowed input. For the card problem, add a row with a repeated number as well.
- Work out the correct output for each chosen input by hand and write it beside the input. Keep the table for testing.
6 Trace
| Row | Allowed? | Output | Explanation |
|---|---|---|---|
| 4 9 2 | yes | 9 | Three cards. 9 is larger than 4 and larger than 2. |
| 7 | yes | 7 | One card is the smallest allowed row. Its number is the largest in the row. |
| 5 5 3 | yes | 5 | The largest number appears twice. The output is still one number, 5. |
| no cards | no | none | The statement allows one or more cards. An empty row is outside the problem, so no output is defined for it. |
7 The trap
The common slip is writing steps before the limits are settled. A statement that says a row of cards and stops there leaves the empty row open. One reader decides the output is 0, and another decides there is none. Decide which inputs are allowed and write the decision into the statement. In this unit the empty row is not allowed.
A second slip is naming the wrong output. The largest number in 5 5 3 is 5. The position of the largest card is a different output, and for this row two positions hold it.
Quick checks
Quick check 1 of 2
A task invented for this question: you are given a list of one or more whole-number scores, and you must report how many of them are 50 or more. Name the input, its limits and the output. Then give the output for the list 50 49 72.
Show the steps and the answer
Input: a list of scores. Limits: one or more whole numbers. Output: how many of the scores are 50 or more. For 50 49 72 the output is 2.
Why: 50 and 72 are each 50 or more, and 49 is not, so the count is 2.
Source: College Board, AP Computer Science Principles Course and Exam Description
Quick check 2 of 2
The card problem allows a row of one or more cards. Three rows are offered: 8, 2 2 and a row with no cards. Which are allowed inputs, and what is the output for each allowed row?
Show the steps and the answer
8 and 2 2 are allowed. Their outputs are 8 and 2. The row with no cards is not allowed, so it has no output.
Why: The statement asks for one or more cards. One card meets that limit, and so do two equal cards. An empty row does not.
Sources
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. 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.
Lesson 2 of 5
Write an unambiguous instruction
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
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.
Lesson 3 of 5
Trace a decision
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
A library desk has a rule for late books, invented for this lesson. A book returned on time costs nothing. A book up to a week late costs a small fee, and a book more than a week late costs a larger one. The clerk asks one question at a time and acts on the answer.
In an algorithm, selection means that a condition decides which steps are carried out. A condition is a question whose answer is true or false, like the predicates in the last unit.
The late-fee procedure has five steps. Step 1: read the number of days late. Step 2: if the number is 0, the fee is 0 coins, and go to step 5. Step 3: if the number is 7 or less, the fee is 1 coin, and go to step 5. Step 4: the fee is 3 coins. Step 5: announce the fee and stop. Steps 2 and 3 are selection steps, because a condition decides whether their action is carried out.
To trace a procedure is to carry it out by hand for one input and write down what happens at each step.
Remember one sentence: at a decision, write the condition with the real values, then true or false. That method is this lesson's own. For selection, find essential knowledge AAP-2.G.1 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of four inputs to the late-fee procedure. For 0 days late the condition in step 2 is true and the fee is 0. For 3 days late the condition in step 3 is the first true one and the fee is 1. For 7 days late the condition in step 3 is again the first true one and the fee is 1. For 8 days late neither condition is true and the fee is 3. The rows for 7 and 8 are marked.
3 Why
A condition is worked out with the values the procedure holds at that moment. For 8 days late, step 2 asks whether 8 is 0. The answer is false, so the action is skipped and the next step in order is carried out. Selection picks the parts of an algorithm to carry out from whether a condition is true or false.
The words go to step 5 matter as much as the conditions. For 0 days late, step 2 sets the fee to 0 and jumps to step 5. Without that jump the tracer would reach step 3, where 0 is 7 or less, and the fee would change to 1. The order of the steps is part of the algorithm.
4 The rule
At a selection step, work out the condition with the current values each time the step is reached.
If it is true, carry out the action the step gives for true. If it is false, carry out the step's otherwise action when it has one. After either, follow any instruction about where to go next, and continue with the next step in order when there is none. The condition decides which action is carried out.
5 How
- Draw a table with a column for the step, a column for the condition with its values filled in and a column for what the procedure holds after the step.
- Write the input above the table.
- Carry out the steps in order. At a selection step, copy the condition with the real values in place of the names.
- Write true or false beside the condition. If it is true, do the action the step gives for true. If it is false, do the step's otherwise action when it has one. After either, follow any instruction about where to go next, and move to the next step in order when there is none.
- Stop at the step that says stop. The output is what that step announces.
- Trace again with inputs that take the other paths: one input that makes each selection step's action run, and one for which no condition is true. For the late-fee procedure,
0,3and8days late do that.
6 Trace
| Step | Condition | Fee | Explanation |
|---|---|---|---|
| 1 | — | not set | The input is 8 days late. |
| 2 | 8 is 0: false | not set | The condition is false. Skip the action and go on to step 3. |
| 3 | 8 is 7 or less: false | not set | False again. Go on to step 4. |
| 4 | — | 3 | Step 4 has no condition. The fee is 3 coins. |
| 5 | — | 3 | Announce 3 and stop. |
7 The trap
Do not skip a step because the answer seems clear. Carry out every step the procedure reaches, including the ones whose condition is false.
The opposite slip is carrying out an action whose condition was false, or going on to step 3 after step 2 has said go to step 5. For 0 days late that slip announces 1 coin where the rule gives 0. Sequencing means carrying out the steps in the order the procedure gives them. Follow each jump as written.
Quick checks
Quick check 1 of 2
Trace the late-fee procedure for 7 days late. Which condition is false, which is true, and what fee is announced?
Show the steps and the answer
The condition in step 2 is false and the condition in step 3 is true. The fee announced is 1 coin.
Why: 7 is not 0, so step 2's action is skipped. 7 is 7 or less, so step 3 sets the fee to 1 and jumps to step 5.
Source: College Board, AP Computer Science Principles Course and Exam Description
Quick check 2 of 2
A tracer forgets the words go to step 5 at the end of step 2 and carries on in order. What fee do they announce for 0 days late, and what fee does the procedure as written announce?
Show the steps and the answer
They announce 1 coin. The procedure as written announces 0 coins.
Why: Step 2 sets the fee to 0. Without the jump the tracer reaches step 3, where 0 is 7 or less, so the fee becomes 1 and step 3 sends them to step 5.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 1 Dec 2026.
Lesson 4 of 5
Explain when a repetition ends
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
A cashier counting a drawer of coins repeats one small action: take a coin and add it to the count. Nobody tells the cashier how many times to do it. The drawer does. When it is empty, the counting ends.
In an algorithm, iteration is a part that repeats. It repeats a set number of times or until a condition is met. The repeating part is often called a loop, and one trip through it is a pass.
In the largest-card procedure from lesson 2, steps 2 to 4 form a loop. Step 2 holds the stopping condition: when no card is left, go to step 5. Step 1 uses the first card, and each later card is compared once in step 3. The Field Guide counts its own example the same way: given 10 items, its loop runs 9 times.
Remember one sentence: to explain why a loop ends, name its stopping condition and say why it is reached. For iteration, find essential knowledge AAP-2.J.1 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of row sizes and the number of comparisons the largest-card procedure makes in step 3. A row of 1 card needs 0 comparisons. A row of 2 cards needs 1. A row of 3 cards needs 2. A row of 10 cards needs 9. The last row is marked.
3 Why
A loop ends for a reason you can point to. In the largest-card procedure the reason has two parts. The row holds a fixed number of cards, and every pass takes one card that is never put back. After the last card has been taken, step 2 finds no card and sends the reader to step 5.
A loop whose stopping condition can never become true does not end. That is an infinite loop. Suppose step 2 told the reader to look at the next card and leave it where it is, without moving along the row. On a row of two or more cards, the reader would compare the same card with the noted number on every pass, and the row would never run out.
4 The rule
A loop repeats its steps until its stopping condition is met.
If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.
5 How
- Find the steps that repeat, and mark the step that sends the reader back.
- Find the stopping condition, which is the test that lets the reader leave the loop.
- Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.
- Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.
- Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.
- Write one sentence that names the stopping condition and the step that moves toward it.
6 Trace
| Step 2 reached | Card taken | Cards left | Leave the loop? | Explanation |
|---|---|---|---|---|
| before the loop | 4 | 2 | — | Step 1 uses the first card. Two cards are left. |
| first time | 9 | 1 | no | Step 2 takes a card, so one is left. Step 3 compares 9 with the noted number. |
| second time | 2 | 0 | no | Step 2 takes the last card. Step 3 compares 2 with the noted number. |
| third time | none | 0 | yes | Step 2 finds no card left. The stopping condition is met, so go to step 5. |
7 The trap
A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.
The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes 1, then repeats two steps: add 2 to the number, and stop if the number is 10. The number goes 3, 5, 7, 9, 11 and on, and never equals 10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes.
Quick checks
Quick check 1 of 2
The largest-card procedure is carried out on a row of 5 cards. How many times is the comparison in step 3 carried out, and which step ends the loop?
Show the steps and the answer
Step 3 is carried out 4 times. Step 2 ends the loop when no card is left.
Why: Step 1 uses the first card without a comparison. Each of the other 4 cards is compared once in step 3. After the last card, step 2 finds none and goes to step 5.
Quick check 2 of 2
A procedure invented for this question has four steps. Step 1: write 10. Step 2: if the number is 0, stop. Step 3: add 1 to the number. Step 4: go back to step 2. Does the loop end? Rewrite step 3 so that it does.
Show the steps and the answer
It does not end. Rewrite step 3 as: subtract 1 from the number.
Why: The stopping condition tests for 0. Adding 1 moves the number away from 0 on every pass: 11, 12, 13 and so on. Subtracting 1 moves it toward 0, which it reaches after 10 passes.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. 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.
Lesson 5 of 5
Check a procedure against a counterexample
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
A procedure can look right and be wrong. One way to find out is to carry it out on inputs chosen to make it fail.
Testing uses chosen inputs to check that a procedure produces the expected outputs. An allowed input for which the procedure gives a wrong output is a counterexample. The mistake it exposes is a logic error, which is a mistake in an algorithm that makes it behave incorrectly.
One counterexample is enough to show that a procedure does not solve its problem, because an algorithm gives the correct result every time.
Here is a changed version of the largest-card procedure with a logic error in its first step. Step 1: write 0 on the slip as the noted number. Step 2: if no card is left in the row, go to step 5, and otherwise take the next card, starting with the first. 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. Testing with chosen inputs is how this lesson finds the error.
Remember one sentence: one wrong output outweighs any number of right ones. For testing, find essential knowledge CRD-2.J.1 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of four tests of the changed procedure, with the expected output and the announced output of each. For the row 4 9 2 both are 9. For the row 7 both are 7. For the row 5 5 3 both are 5. For the row -3 -8 the expected output is -3 and the procedure announces 0. That last row is marked as the counterexample.
3 Why
The changed procedure starts with a noted number of 0, whether or not any card shows 0. For a row that holds a number above 0, the first such card replaces the 0 on the slip, and the mistake stays hidden. For the row -3 -8, no card is larger than 0, so the slip is never changed and the procedure announces 0, a number that is not in the row.
Three tests passed before the fourth showed the mistake. Passing tests show that a procedure works for the inputs tried. They do not show that it works for every allowed input. That is one reason to include inputs at the extremes of what is allowed among the tests.
4 The rule
A test case is an allowed input together with the output expected for it.
In this unit the expected output is worked out by hand before the procedure is carried out. A procedure passes a test when it announces that output. An allowed input for which it announces anything else is a counterexample, and one counterexample shows that the procedure does not solve the problem. Passing tests cover only the inputs tried.
5 How
- Take the table of inputs and correct outputs made with the problem statement, as in lesson 1.
- Add inputs at the extremes of what is allowed, such as the smallest allowed row. Add unusual rows too: a row with a repeated value, and a row whose numbers are all below zero.
- Trace the procedure on each input, and write what it announces beside the expected output.
- Mark each row pass or fail. A failed row is a counterexample.
- For a counterexample, identify the error and correct it. One way to find it is to look for the first step where the trace differs from a correct run. Correct that step and any later step that depends on it.
- Carry out every test again after the correction, including the tests that passed before.
6 Trace
| Step | Card taken | Noted number | Explanation |
|---|---|---|---|
| 1 | — | 0 | The slip starts at 0, a number that is on no card of this row. |
| 2 | -3 | 0 | Take the first card. |
| 3 | — | 0 | The card shows -3, and -3 is not larger than 0. The slip stays as it is. |
| 4 | — | 0 | Go back to step 2. |
| 2 | -8 | 0 | Take the next card. |
| 3 | — | 0 | The card shows -8, and -8 is not larger than 0. The slip stays as it is. |
| 4 | — | 0 | Go back to step 2. |
| 2 | none | 0 | No card is left. Go to step 5. |
| 5 | — | 0 | Announce 0. The expected output is -3, so this row is a counterexample. |
7 The trap
After several passing tests it is tempting to call the procedure correct. The three rows that pass all hold a number above 0, which hides the mistake. The changed procedure fails only on rows whose numbers are all below 0. Choose tests that differ from each other in kind, including inputs at the extremes of what is allowed.
A second slip is patching the counterexample alone, for instance with a new step that announces -3 for that one row. The row -9 -4 would then fail in the same way. Correct the steps that cause the error by restoring the first two steps of the largest-card procedure of lesson 2. Step 1 takes the first card and writes its number on the slip, and step 2 goes on with the cards that are left.
Quick checks
Quick check 1 of 2
Trace the changed procedure, the one that starts with 0 on the slip, on the row -5 -1 -7. What does it announce, what is the correct output, and is the row a counterexample?
Show the steps and the answer
It announces 0. The correct output is -1. The row is a counterexample.
Why: No card is larger than 0, so the slip never changes. The largest number in the row is -1, and the procedure announces something else.
Source: College Board, AP Computer Science Principles Course and Exam Description
Quick check 2 of 2
The changed procedure passes the tests 4 9 2, 7 and 5 5 3. A classmate says that three passes show it is correct. What is wrong with that claim, and what kind of row tests it?
Show the steps and the answer
Passing tests cover only the rows tried. A row in which every number is below 0, such as -3 -8, makes the procedure announce 0.
Why: A procedure solves the problem only if it is right for every allowed row. The three rows all hold a number above 0, so none of them reaches the case that the first step gets wrong.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. 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.