← ACARA Digital Technologies Years 7–8

Tracing a loop by hand

A table with one row per loop repeat, showing the counter, other variables and any output, filled in by hand until the condition is false.

Understanding Tracing a loop by hand in ACARA Digital Technologies Years 7–8

What it does: The trace table shows i as 2, 4, 6. The prediction is written first, then compared with the real console output. A trace table has one row per repeat of the loop. This lets you see how each value changes along the way. i starts at 1 and the loop runs while i <= 3, so it repeats 3 times. When i becomes 4 the condition is false and the loop stops. Repeat 1: total = 0 + 1 = 1, then n becomes 2. Repeat 2: total = 1 + 2 = 3, then n becomes 3. The total adds up each time.

Common mistakes: The variable is misspelled as totl, so JavaScript throws a ReferenceError. It should be console.log(i, total); so the output matches the trace table columns i and total. count + 1; works out a value but never stores it, so count stays 0 in every row of the trace table and the loop never ends. It should be count++; (or count += 1;).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 624–634) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which describes how to trace a loop by hand using a trace table?
Write one row for each repeat, showing the counter, other variables and any outputWrite one row only, showing the final value of each variable
Matches
// Trace of: for (let i = 1; i < 4; i++) // Rows: i=1, i=2, i=3, i=4
↓
The trace includes a row for i = 4, but the condition i < 4 is false when i is 4, so the loop body never runs for it. Only 3 rows are needed.
Applying
Meanings
Term
trace table
Meaning
A table with one row per loop repeat, showing the counter, other variables and any output, filled in by hand until the condition is false.
Practice
let total = 0;
Testing
Gaps
for (let i = 1; i <= 3; ) {
Check Answer
Spots
3 total += i;
4 console.log(i, totl);

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Stag' badge — Forest Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which describes how to trace a loop by hand using a trace table?
A. Write one row for each repeat, showing the counter, other variables and any output ✓
B. Write one row only, showing the final value of each variable
A trace table has one row per repeat of the loop. This lets you see how each value changes along the way.
2. for (let i = 1; i <= 3; i++) { console.log(i); } How many rows does the trace table need for the repeats?
A. 4 rows: i is 0, then 1, then 2, then 3
B. 3 rows: i is 1, then 2, then 3 ✓
i starts at 1 and the loop runs while i <= 3, so it repeats 3 times. When i becomes 4 the condition is false and the loop stops.
3. When do you stop adding rows to a trace table?
A. When the loop condition becomes false ✓
B. When the counter reaches 10
A loop keeps repeating while its condition is true. You stop tracing when the condition is false, not at a fixed number.
4. let total = 0; let n = 1; while (n <= 3) { total += n; n++; } After the second repeat, what are the values in the trace table?
A. n is 2 and total is 3 (before n++ runs), then n becomes 3 ✓
B. n is 2 and total is 2
Repeat 1: total = 0 + 1 = 1, then n becomes 2. Repeat 2: total = 1 + 2 = 3, then n becomes 3. The total adds up each time.
5. Predicting the output before running a program is useful because...
A. The computer no longer needs to run the code
B. You can compare your trace with the real output to find mistakes ✓
If your prediction and the real output differ, either your trace or the code has a mistake. Comparing them helps you find it.

Match the Pairs

for (let i = 1; i <= 3; i++) { console.log(i); } → A trace table for this loop has 3 rows: i is 1, then 2, then 3, with output 1, 2, 3. It stops when i becomes 4.
let total = 0; let n = 1; while (n <= 3) { total += n; n++; } → Trace table columns: n and total. Row 1: n=1, total=1. Row 2: n=2, total=3. Row 3: n=3, total=6. Then n is 4 and the loop stops.
Predicting the output by hand: before running, fill in a trace table, then write down what the loop will print. → for (let i = 2; i <= 6; i += 2) { console.log(i); } // Prediction: 2, 4, 6
Comparing your trace with the real output: if the console output differs from your table, you made a tracing mistake or the code has a bug. → let count = 3; while (count > 0) { console.log(count); count--; } // Trace: 3, 2, 1
// Trace of: for (let i = 1; i < 4; i++) // Rows: i=1, i=2, i=3, i=4 → The trace includes a row for i = 4, but the condition i < 4 is false when i is 4, so the loop body never runs for it. Only 3 rows are needed.
// Trace of: let total = 0; // for (let i = 1; i <= 3; i++) { total += i; } // Rows: total=1, total=2, total=3 → The total was not added up. It should keep a running sum: 1, then 3, then 6, because each repeat adds i to the previous total.

Key Term

trace table
A table with one row per loop repeat, showing the counter, other variables and any output, filled in by hand until the condition is false.

Fill in the Blanks

1. for (let i = 1; i <= 3; i++) { console.log(i); }
Hint: Type the update that adds 1 to the counter after each repeat. This is how the i column changes from row to row in a trace table.
2. for (let i = 1; i <= 3; i++) { console.log(i); }
Hint: Type the comparison operator that keeps the loop running when i is equal to 3, so the trace table gets a row for i = 3.
3. let total = 0; let n = 1; while (n <= 3) { total += n; n++; }
Hint: Type the shorthand operator that adds n to total on every repeat. This is the change you record in the total column of the trace table.
4. let total = 0; for (let i = 1; i <= 4; i++) { total += i; } console.log(total);
Hint: Type the variable name whose final trace table value is the program's output.
5. let n = 3; while (n > 0) { console.log(n); n--; } // trace rows: n = 3, 2, 1
Hint: Type the keyword that repeats the block while its condition is true. You stop adding rows to the trace table when the condition becomes false.

Spot the Error

Question 1
A let total = 0;
B for (let i = 1; i <= 3; i++) {
C total += i;
D console.log(i, totl);
E }
Line D — The variable is misspelled as totl, so JavaScript throws a ReferenceError. It should be console.log(i, total); so the output matches the trace table columns i and total.
Question 2
A let count = 0;
B while (count < 3) {
C count + 1;
D console.log(count);
E }
Line C — count + 1; works out a value but never stores it, so count stays 0 in every row of the trace table and the loop never ends. It should be count++; (or count += 1;).
Question 3
A let total = 0;
B for (let i = 1, i <= 4, i++) {
C total += i;
D }
E console.log(total);
Line B — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 1; i <= 4; i++) {.
Question 4
A let total;
B for (let i = 1; i <= 3; i++) {
C total += i;
D }
E console.log(total);
Line A — Without = 0, total starts as undefined, so the first trace row would be undefined + 1 = NaN. The total must start at 0: let total = 0;.
Question 5
A let count = 5;
B while (count < 0) {
C console.log(count);
D count -= 1;
E }
Line B — count starts at 5, so count < 0 is false straight away and the trace table would have no rows. To count down the condition should be count > 0.

Student with a class code? Join your class →