Pilot · L04 · 60 minutes
Iteration, debugging and testing
Trace counted and conditional loops to their stopping state, repair an accumulator that resets every iteration, and design tests with expected results on and either side of a threshold.
Topics: 1.4 Identifying and Correcting Errors · 3.8 Iteration · 3.9 Developing Algorithms
Goals and vocabulary
By the end you can:
- Trace REPEAT n TIMES and REPEAT UNTIL loops, including the value that stops the loop.
- Explain why a loop never terminates and change the update so it does.
- Distinguish syntax, logic and run-time errors from a description of what happens.
- Locate and repair a logic error in an accumulator using a trace.
- Choose tests with expected outputs, including either side of a threshold, and find an input that distinguishes two algorithms.
- Iteration
- Repeating a block of statements, either a set number of times or until a condition is met.
- Accumulator
- A variable set before a loop and updated inside it to build a total or count.
- Infinite loop
- A loop whose stopping condition is never met, so it never ends.
- Logic error
- A mistake that lets the program run but produces incorrect results.
- Syntax error
- A mistake in the rules of the language that prevents the program from running.
- Test case
- A chosen input with the expected output worked out from the specification.
Counted loops and accumulators
REPEAT n TIMES runs its block exactly n times. An accumulator is a variable that builds up a result across iterations: it is set once before the loop and updated inside it.
total ← 0REPEAT 3 TIMES{ points ← INPUT() total ← total + points}DISPLAY(total)With inputs 4, 6 and 5 this displays 15. Two details matter: total starts at 0 before the loop (not inside it), and the update adds to the old total rather than replacing it.
total = 0for _ in range(3): points = int(input()) total = total + pointsprint(total)REPEAT UNTIL and stopping
REPEAT UNTIL (condition) checks the condition before each iteration and stops as soon as it is true. The block may run zero times if the condition is already true. Something inside the block must eventually make the condition true, or the loop never stops.
x ← 1REPEAT UNTIL (x > 20){ x ← x * 3}DISPLAY(x)| Check | x before check | x > 20? | Action |
|---|---|---|---|
| 1 | 1 | false | x ← 3 |
| 2 | 3 | false | x ← 9 |
| 3 | 9 | false | x ← 27 |
| 4 | 27 | true | Stop. Displays 27. |
Kinds of error
| Kind | What happens | Example |
|---|---|---|
| Syntax error | The program does not follow the language rules and will not run. | A missing closing bracket or misspelled keyword. |
| Logic error | The program runs but produces the wrong result. | total ← points instead of total ← total + points. |
| Run-time error | The program starts but fails while running. | Dividing by zero, or reading list item 5 of a 3-item list. |
| Overflow error | A value is too large for the space available to store it. | See L07. |
Logic errors are the hardest to find because nothing announces them. The only way to see one is to compare the program’s actual output with the expected output for a chosen input.
Worked example: repairing an accumulator
Worked example
This program should count how many of 4 entered temperatures are above 30. Predict what it displays for 31, 25, 35, 40 before reading on.
REPEAT 4 TIMES{ hot ← 0 t ← INPUT() IF (t > 30) { hot ← hot + 1 }}DISPLAY(hot)| Iteration | t | hot after reset | hot at end |
|---|---|---|---|
| 1 | 31 | 0 | 1 |
| 2 | 25 | 0 | 0 |
| 3 | 35 | 0 | 1 |
| 4 | 40 | 0 | 1 |
It displays 1, but the expected result is 3. The trace shows the cause: hot ← 0 is inside the loop, so the count is reset every iteration. Moving hot ← 0 above REPEAT fixes it. Re-run the same test: 1, 1, 2, 3 — displays 3.
Choosing tests with expected results
A test is an input together with the output you expect, worked out from the specification before running the program. Good test sets include typical values, values on and either side of each boundary, and unusual cases such as no data or a loop that should run zero times.
| Inputs | Expected | Purpose |
|---|---|---|
| 31, 25, 35, 40 | 3 | Typical mix. |
| 30, 30, 30, 30 | 0 | On the boundary: 30 is not above 30. |
| 31, 31, 31, 31 | 4 | Just above the boundary, every iteration counts. |
| 10, 20, 0, -5 | 0 | No matches. |
To compare two algorithms meant to do the same job, look for an input where they produce different results. If you find one, at least one is wrong for that input.
Explore each concept
Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.
- Error classification
- Expected versus observed output
- Distinguishing tests and regression
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- FaultlogicWaiting
- Boundary test value (0–100)5Waiting
Ready to explore: Locate syntax, execution and logic errors
Why: Change an input, predict the first effect, then advance one step.
Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 15,5,1Waiting
- Input 25Waiting
- Input 3boundaryWaiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Try repeated boundary values, no matches and an empty list.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 110Waiting
- Input 23Waiting
- Input 3at-mostWaiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Compare equal and at-most with start 10 and decrement 3, then try start 0.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 12,7,3,7,0Waiting
- Input 25Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Try repeated boundary values, no matches and an empty list.
Common misconceptions
One plausible output proves the program
The belief: It gave a sensible answer, so it works.
What is actually true: A wrong program can give the right answer for some inputs. Check expected against actual for several tests, including boundaries.
Check yourself: Which of your tests would fail if the accumulator reset every iteration?
REPEAT UNTIL stops at the limit value
The belief: REPEAT UNTIL (x > 20) ends with x equal to 20.
What is actually true: It stops at the first value that makes the condition true, which may be well past the limit.
Check yourself: What value did x hold at the check that ended the loop?
It does not matter where the accumulator starts
The belief: total ← 0 can go anywhere before it is used.
What is actually true: Inside the loop it resets every iteration and throws away the running total.
Check yourself: How many times does the line total ← 0 run?
Guided practice
Guided: predict, trace, repair
With a partner, predict each loop’s output, trace it, then fix Fragment B so it never runs forever.
sum ← 0n ← 2REPEAT 3 TIMES{ sum ← sum + n n ← n + 2}DISPLAY(sum)seats ← 25REPEAT UNTIL (seats = 0){ seats ← seats - 2}DISPLAY("Full")- Write a prediction for Fragment A, then trace sum and n through each iteration.
- Trace the first five iterations of Fragment B and explain why it never stops.
- Give two different repairs for Fragment B and say which you prefer.
- For your preferred repair, state the value of seats when the loop stops.
Independent practice
Independent: test a threshold program
A program reads 5 scores and should count how many are at least 70. A student wrote the condition IF (score > 70).
- Write four tests (five scores each) with expected counts from the specification.
- Make sure one test has a score exactly 70 and one has 69 and 71.
- Say which of your tests the student’s program fails and what it displays instead.
- Explain why the test 50, 60, 80, 90, 100 alone would not reveal the error.
- Describe one test that checks the accumulator is not reset inside the loop.
Exit check
- For x ← 2, REPEAT UNTIL (x > 30) { x ← x * 2 }, state the value of x when the loop stops and how many times the block ran.
- A program should give free entry to anyone aged 65 or older. Give one test on the boundary and one just below it, with the expected results.
Check yourself
Practice only: answers are checked in your browser and nothing is saved or sent to your teacher. Each explanation links back to the section it practises.
Lesson practice
Your practice so far
Check an answer to see its rationale.
Apply it somewhere new
Your practice so far
Check an answer to see its rationale.