Computer Science · Algorithms and tracing
Why does my trace table always go wrong on the last iteration?
Because you are almost certainly checking the condition at the wrong moment, and the last row is the only place that difference shows up.
The thing that is actually going wrong
Take a loop like this:
count <- 1
total <- 0
WHILE count <= 5 DO
total <- total + count
count <- count + 1
ENDWHILE
OUTPUT total
Most students trace this by writing a row for each pass and updating both
variables, which is right. The error creeps in at the end. After the fifth pass,
count becomes 6. A lot of trace tables stop there, because five passes felt
like the whole job.
But the loop has not finished. It goes back to the top, checks 6 <= 5, finds
it false, and then exits. That final check is a step, and if the question asks
for the value of count when the loop terminates, the answer is 6, not 5.
The completed table:
| count | total | condition |
|---|---|---|
| 1 | 0 | true |
| 2 | 1 | true |
| 3 | 3 | true |
| 4 | 6 | true |
| 5 | 10 | true |
| 6 | 15 | false, exit |
Output is 15. count finishes at 6.
Why this specific mistake is so common
Two reasons, and they compound.
The first is that in your head, the loop "runs five times", so five rows feels complete. The sixth row does not correspond to any work being done, so it feels like it should not exist. But the trace table is not a record of work done. It is a record of the machine's state at each step, and checking a condition and failing it is a step.
The second is that when you write code in an editor you never see this. The loop exits, the program carries on, and the final value of the counter is invisible unless you print it. Tracing on paper is the only place it becomes a question, which is exactly why the exam asks it there.
How to stop doing it
Write the condition as a column. Not a mental check, an actual column in the table, filled in for every row including the last. The moment "condition" is something you have to write down, you cannot skip the row where it turns false.
Then, before you hand the table over, ask one question: does my last row show the loop being told to stop? If it does not, the table is incomplete.
The variation that catches people out
Swap WHILE for REPEAT ... UNTIL and the same students make the opposite
error. A REPEAT loop checks its condition at the end of the pass, which
means the body always runs at least once, even when the condition is already
true going in. Students who have drilled WHILE tracing sometimes add a
phantom condition-check row at the top, which is just as wrong in the other
direction.
The fix is the same: write the condition column, and put the check where the construct actually puts it.
What this is worth in the exam
Trace table questions are typically three to six marks and they are, with practice, close to free. There is no cleverness in them. They reward exactly one thing, which is doing the bookkeeping carefully when you already believe you know the answer.
That last clause is the whole difficulty. Strong programmers are worse at trace tables than average students surprisingly often, because they read the algorithm, understand what it does, and write down what they believe the output should be rather than working through each iteration. The examiner writes these questions with an off-by-one at the boundary precisely to catch that.
Working from a past paper? Mark your attempt against the published mark scheme before reading an answer like this one. Finding out where you lost the marks is worth more than seeing a correct solution, because the correct solution rarely tells you what you did instead.
Related questions
Ordered by how close they are to this one: same topic first, then the same subject.
How do I write a negative number in two's complement without getting confused?
There is a two-step method that always works, and most of the confusion comes from students half-remembering a third step that does not exist.
I wrote working Python and got half marks. Why?
Because the paper asked for pseudocode and marks the structure, not whether the code would run. A correct Python answer can miss several structural marks while…
I keep mixing up xylem and phloem in the exam. Is there a way to stop doing that?
Yes, and it is not a mnemonic. Learn one fact about each and derive the rest, because the two tissues differ in almost every way once you have the starting point.
Why do we bother with depreciation if no money actually leaves the business?
You are right that no cash moves. That is exactly why the concept exists, and seeing the reason makes the whole topic easier to hold onto.
Mean, median or mode: how do I justify which average a question wants?
Look at the distribution, not the data type. The decision turns almost entirely on whether there are extreme values, and a question giving you a skewed data set is…
Why does adding a second lamp in parallel not make both lamps dimmer?
Because in parallel each lamp gets the full supply voltage, regardless of how many others are connected. Adding another branch does not take anything away from the…