Tracing a loop by hand

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Tracing a loop by hand

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
3. When do you stop adding rows to a trace table?
A When the loop condition becomes false
B When the counter reaches 10
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for (let i = 1; i <= 3; i++) { console.log(i); } AThe 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.
2let total = 0; let n = 1; while (n <= 3) { total += n; n++; } Bfor (let i = 2; i <= 6; i += 2) { console.log(i); } // Prediction: 2, 4, 6
3Predicting the output by hand: before running, fill in a trace table, then write down what the loop will print. CTrace 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.
4Comparing 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. DThe 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.
5// Trace of: for (let i = 1; i < 4; i++) // Rows: i=1, i=2, i=3, i=4 Elet count = 3; while (count > 0) { console.log(count); count--; } // Trace: 3, 2, 1
6// Trace of: let total = 0; // for (let i = 1; i <= 3; i++) { total += i; } // Rows: total=1, total=2, total=3 FA 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.

Definitions

trace table

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for (let i = 1; i <= 3; ___) { console.log(i); }
2.
for (let i = 1; i __ 3; i++) { console.log(i); }
3.
let total = 0; let n = 1; while (n <= 3) { total __ n; n++; }
4.
let total = 0; for (let i = 1; i <= 4; i++) { total += i; } console.log(_____);
5.
let n = 3; _____ (n > 0) { console.log(n); n--; } // trace rows: n = 3, 2, 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let total = 0;
B for (let i = 1; i <= 3; i++) {
C total += i;
D console.log(i, totl);
E }
Question 2
A let count = 0;
B while (count < 3) {
C count + 1;
D console.log(count);
E }
Question 3
A let total = 0;
B for (let i = 1, i <= 4, i++) {
C total += i;
D }
E console.log(total);
Question 4
A let total;
B for (let i = 1; i <= 3; i++) {
C total += i;
D }
E console.log(total);
Question 5
A let count = 5;
B while (count < 0) {
C console.log(count);
D count -= 1;
E }

Answer Key

Tracing a loop by hand
Section A: Quick Questions
Q1
A — Write one row for each repeat, showing the counter, other variables and any output
Q2
B — 3 rows: i is 1, then 2, then 3
Q3
A — When the loop condition becomes false
Q4
A — n is 2 and total is 3 (before n++ runs), then n becomes 3
Q5
B — You can compare your trace with the real output to find mistakes
Section B: Match the Pairs
Q1
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.
Q2
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.
Q3
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
Q4
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
Q5
// 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.
Q6
// 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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for (let i = 1; i <= 3; i++) { console.log(i); }
Q2
for (let i = 1; i <= 3; i++) { console.log(i); }
Q3
let total = 0; let n = 1; while (n <= 3) { total += n; n++; }
Q4
let total = 0; for (let i = 1; i <= 4; i++) { total += i; } console.log(total);
Q5
let n = 3; while (n > 0) { console.log(n); n--; } // trace rows: n = 3, 2, 1
Section E: Spot the Error
Q1
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.
Q2
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;).
Q3
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++) {.
Q4
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;.
Q5
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.