Tracing a loop in a trace table

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

Tracing a loop in a trace table

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You are tracing a WHILE loop. When do you test the condition?
A Before each pass of the body
B After each pass of the body
2. Count is 10 before this loop: REPEAT Count ← Count + 1 UNTIL Count > 5. How many times does the body run?
A Zero times
B Once
3. Count is 7 before this loop: WHILE Count < 5 DO. How many times does the body run?
A Zero times
B Once
4. For FOR Counter ← 2 TO 5, which values does Counter take in the trace table?
A 2, 3, 4
B 2, 3, 4, 5
5. Which is the correct way to fill in a trace table for a loop?
A Add a new row on each pass showing the changed values
B Write one row at the end showing only the final values

Match the Pairs

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

Left columnRight column
1WHILE Count < 3 DO AThe condition is tested before the body, so the loop may run zero times. Check it before every pass.
2UNTIL Total >= 10 BTotal ← Total + Count
3A FOR loop trace has one row per pass, with the counter taking each value from start to end CFOR Count ← 1 TO 4
4Record a changed value in a new row each pass, such as a running total going up DThe condition is tested after the body, so the loop always runs at least once. Stop when it becomes true.
5Trace of REPEAT ... UNTIL Count = 3: test Count = 3 before running the body the first time EA REPEAT loop tests its condition after the body, not before. The body must run once first.
6Trace of FOR Count ← 1 TO 3: stop the table after the row where Count = 2 FThe final row was not checked. The loop runs for Count = 3 too, so the last row is missing.

Definitions

Tracing a loop

Fill in the Blanks

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

1.
FOR Count ← 1 __ 4 OUTPUT Count NEXT Count
2.
_____ Num < 3 DO Num ← Num + 1 ENDWHILE
3.
REPEAT Num ← Num + 2 _____ Num >= 7
4.
FOR Count ← 1 TO 3 Total ← _____ + Count NEXT Count
5.
WHILE Num < 10 DO Num ← Num _ 2 ENDWHILE

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 4 TO 1
C Total ← Total + Count
D NEXT Count
E OUTPUT Total
Question 2
A WHILE Num < 3 THEN
B Num ← Num + 1
C OUTPUT Num
D ENDWHILE
Question 3
A Count ← 0
B REPEAT
C Count ← Count + 0
D UNTIL Count = 3
Question 4
A Sum ← 0
B FOR Item ← 1 TO 3
C Sum ← Sum + Item
D NEXT Sum
E OUTPUT Sum
Question 5
A Count ← 3
B WHILE Count < 0 DO
C Count ← Count - 1
D ENDWHILE

Answer Key

Tracing a loop in a trace table
Section A: Quick Questions
Q1
A — Before each pass of the body
Q2
B — Once
Q3
A — Zero times
Q4
B — 2, 3, 4, 5
Q5
A — Add a new row on each pass showing the changed values
Section B: Match the Pairs
Q1
WHILE Count < 3 DO → The condition is tested before the body, so the loop may run zero times. Check it before every pass.
Q2
UNTIL Total >= 10 → The condition is tested after the body, so the loop always runs at least once. Stop when it becomes true.
Q3
A FOR loop trace has one row per pass, with the counter taking each value from start to end → FOR Count ← 1 TO 4
Q4
Record a changed value in a new row each pass, such as a running total going up → Total ← Total + Count
Q5
Trace of REPEAT ... UNTIL Count = 3: test Count = 3 before running the body the first time → A REPEAT loop tests its condition after the body, not before. The body must run once first.
Q6
Trace of FOR Count ← 1 TO 3: stop the table after the row where Count = 2 → The final row was not checked. The loop runs for Count = 3 too, so the last row is missing.
Section C: Definitions
Q1
Tracing a loop — Work through the loop body once per pass, recording changed values in a new row each time. Test the condition before the body for WHILE and after it for REPEAT. Check the final row.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 4 OUTPUT Count NEXT Count
Q2
WHILE Num < 3 DO Num ← Num + 1 ENDWHILE
Q3
REPEAT Num ← Num + 2 UNTIL Num >= 7
Q4
FOR Count ← 1 TO 3 Total ← Total + Count NEXT Count
Q5
WHILE Num < 10 DO Num ← Num * 2 ENDWHILE
Section E: Spot the Error
Q1
B — Count starts at 4 and the end value is 1, so with the default step of +1 the body never runs and the trace table has no loop rows. It should be FOR Count ← 1 TO 4, which gives Count values 1, 2, 3, 4.
Q2
A — A WHILE loop header ends with DO, not THEN (THEN belongs to IF). The condition is tested before each pass of the body, so the line should be WHILE Num < 3 DO.
Q3
C — Adding 0 means Count never changes, so every row of the trace table repeats 0 and the UNTIL test is never true. The loop never ends. It should be Count ← Count + 1.
Q4
D — NEXT must name the loop counter, which is Item. Sum is the running total, not the counter. It should be NEXT Item.
Q5
B — A WHILE loop tests its condition before the body. Count starts at 3, so 3 < 0 is false and the body never runs, leaving no loop rows in the trace table. Since the body counts down, the condition should be Count > 0.