Skip to main content

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.

AP CSP pseudocode
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.

The same program in Python
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.

AP CSP pseudocode
x ← 1REPEAT UNTIL (x > 20){  x ← x * 3}DISPLAY(x)
Trace of REPEAT UNTIL
Checkx before checkx > 20?Action
11falsex ← 3
23falsex ← 9
39falsex ← 27
427trueStop. Displays 27.

Kinds of error

Errors you will meet
KindWhat happensExample
Syntax errorThe program does not follow the language rules and will not run.A missing closing bracket or misspelled keyword.
Logic errorThe program runs but produces the wrong result.total ← points instead of total ← total + points.
Run-time errorThe program starts but fails while running.Dividing by zero, or reading list item 5 of a 3-item list.
Overflow errorA 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.

Buggy counter
REPEAT 4 TIMES{  hot ← 0  t ← INPUT()  IF (t > 30)  {    hot ← hot + 1  }}DISPLAY(hot)
Trace with inputs 31, 25, 35, 40
Iterationthot after resethot at end
13101
22500
33501
44001

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.

Tests for “count temperatures above 30”
InputsExpectedPurpose
31, 25, 35, 403Typical mix.
30, 30, 30, 300On the boundary: 30 is not above 30.
31, 31, 31, 314Just above the boundary, every iteration counts.
10, 20, 0, -50No 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
Open this example on its own

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

Ready to explore: Locate syntax, execution and logic errors
  1. FaultlogicWaiting
  2. 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

Ready to experiment
  1. Input 15,5,1Waiting
  2. Input 25Waiting
  3. 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

Ready to experiment
  1. Input 110Waiting
  2. Input 23Waiting
  3. 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

Ready to experiment
  1. Input 12,7,3,7,0Waiting
  2. 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.

Fragment A
sum ← 0n ← 2REPEAT 3 TIMES{  sum ← sum + n  n ← n + 2}DISPLAY(sum)
Fragment B
seats ← 25REPEAT UNTIL (seats = 0){  seats ← seats - 2}DISPLAY("Full")
  1. Write a prediction for Fragment A, then trace sum and n through each iteration.
  2. Trace the first five iterations of Fragment B and explain why it never stops.
  3. Give two different repairs for Fragment B and say which you prefer.
  4. 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).

  1. Write four tests (five scores each) with expected counts from the specification.
  2. Make sure one test has a score exactly 70 and one has 69 and 71.
  3. Say which of your tests the student’s program fails and what it displays instead.
  4. Explain why the test 50, 60, 80, 90, 100 alone would not reveal the error.
  5. Describe one test that checks the accumulator is not reset inside the loop.

Exit check

  1. 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.
  2. 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

  1. Question 1 of 5What does this program display?

    Select one answer.

    AP CSP pseudocode
    total ← 0n ← 1REPEAT 4 TIMES{  total ← total + n  n ← n + 2}DISPLAY(total)
  2. Question 2 of 5What does this program display?

    Select one answer.

    AP CSP pseudocode
    x ← 1REPEAT UNTIL (x > 20){  x ← x * 3}DISPLAY(x)
  3. Question 3 of 5This loop never stops. Which explanation is correct?

    Select one answer.

    AP CSP pseudocode
    count ← 10REPEAT UNTIL (count = 0){  DISPLAY(count)  count ← count - 3}
  4. Question 4 of 5This program should display the sum of three entered numbers. For inputs 4, 6, 5 it displays 5. What is the error?

    Select one answer.

    AP CSP pseudocode
    total ← 0REPEAT 3 TIMES{  points ← INPUT()  total ← points}DISPLAY(total)
  5. Question 5 of 5Program A shows the message “missing closing bracket on line 6” and does not start. Program B runs and displays 7 when the expected output is 12. Which description is correct?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Apply it somewhere new

  1. Question 1 of 3Two students wrote algorithms meant to double a whole number n. Which two test inputs show that Algorithm A is wrong? Select two.

    Select exactly two answers (0 of 2 selected).

    AP CSP pseudocode
    Algorithm Aresult ← n * nAlgorithm Bresult ← n + n
  2. Question 2 of 3A club accepts students aged 14 or older. Which pair of tests best checks that the program uses the boundary correctly?

    Select one answer.

  3. Question 3 of 3Shipping is free for orders of 50 dollars or more; otherwise it costs 4 dollars. Which test case has the correct expected shipping cost?

    Select one answer.

Your practice so far

Check an answer to see its rationale.