Tracing a FOR loop

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

Tracing a FOR loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace table is being filled in for FOR Count ← 1 TO 5. Which values does Count take, in order?
A 1, 2, 3, 4
B 1, 2, 3, 4, 5
2. How many times does the loop body run for FOR Num ← 3 TO 7?
A 5 times (Num = 3, 4, 5, 6, 7)
B 4 times (7 - 3)
3. When tracing a FOR loop, how are the passes recorded in the trace table?
A Each pass gets its own row
B The whole loop is written in one row
4. Should the loop variable have its own column in a trace table?
A No, the FOR loop controls it so it is not recorded
B Yes, the loop variable is a variable and gets a column
5. Which values does X take in FOR X ← 0 TO 10 STEP 5?
A 0, 5, 10
B 0, 5, 10, 15

Match the Pairs

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

Left columnRight column
1FOR Count ← 1 TO 3 ATrace this loop with Num = 2, then 6, then 10, because STEP 4 adds 4 each time, giving 3 passes.
2FOR Num ← 2 TO 10 STEP 4 BSTEP 2 was ignored. The loop variable should go up by 2 each pass, giving 1, 3, 5, 7, 9 and not every number.
3Every pass of the loop body gets its own row, with the loop variable column showing the current value for that pass. CFOR Count ← 1 TO 4 OUTPUT Count * 2 NEXT Count
4A FOR loop that counts down uses a negative STEP, so the loop variable is traced with 5, 4, 3, 2, 1. DFOR Num ← 5 TO 1 STEP -1 OUTPUT Num NEXT Num
5FOR Count ← 1 TO 3 gives rows with Count = 1, 2, 3, 4 EThe trace goes one value too far. The loop variable stops at the end value 3, so there are only 3 passes and no row for 4.
6FOR Num ← 1 TO 9 STEP 2 gives rows with Num = 1, 2, 3, 4, 5 FTrace this loop with Count = 1, then 2, then 3, running the loop body once for each value and using one row per pass.

Definitions

FOR Count ← 1 TO 5

Fill in the Blanks

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

1.
FOR Count ← 1 __ 5 OUTPUT Count NEXT Count
2.
FOR Count ← 1 TO 5 OUTPUT Count ____ Count
3.
FOR Count ← 1 TO 10 STEP _ OUTPUT Count NEXT Count
4.
FOR Num ← 1 TO 3 Total ← Total + ___ NEXT Num
5.
FOR Count ← 5 TO 1 STEP __ OUTPUT Count NEXT Count

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 1 TO 4
C Total ← Count
D NEXT Count
E OUTPUT Total
Question 2
A // Count takes 1, 2, 3, 4, 5
B FOR Count ← 1 TO 4
C OUTPUT Count
D NEXT Count
Question 3
A FOR Num ← 1 TO 3
B OUTPUT Num * 2
C NEXT Count
D OUTPUT "Finished"
Question 4
A FOR Count = 1 TO 3
B OUTPUT Count
C NEXT Count
Question 5
A Product ← 1
B FOR Num ← 1 TO 3
C Product ← Product * Num
D ENDFOR
E OUTPUT Product

Answer Key

Tracing a FOR loop
Section A: Quick Questions
Q1
B — 1, 2, 3, 4, 5
Q2
A — 5 times (Num = 3, 4, 5, 6, 7)
Q3
A — Each pass gets its own row
Q4
B — Yes, the loop variable is a variable and gets a column
Q5
A — 0, 5, 10
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 3 → Trace this loop with Count = 1, then 2, then 3, running the loop body once for each value and using one row per pass.
Q2
FOR Num ← 2 TO 10 STEP 4 → Trace this loop with Num = 2, then 6, then 10, because STEP 4 adds 4 each time, giving 3 passes.
Q3
Every pass of the loop body gets its own row, with the loop variable column showing the current value for that pass. → FOR Count ← 1 TO 4 OUTPUT Count * 2 NEXT Count
Q4
A FOR loop that counts down uses a negative STEP, so the loop variable is traced with 5, 4, 3, 2, 1. → FOR Num ← 5 TO 1 STEP -1 OUTPUT Num NEXT Num
Q5
FOR Count ← 1 TO 3 gives rows with Count = 1, 2, 3, 4 → The trace goes one value too far. The loop variable stops at the end value 3, so there are only 3 passes and no row for 4.
Q6
FOR Num ← 1 TO 9 STEP 2 gives rows with Num = 1, 2, 3, 4, 5 → STEP 2 was ignored. The loop variable should go up by 2 each pass, giving 1, 3, 5, 7, 9 and not every number.
Section C: Definitions
Q1
FOR Count ← 1 TO 5 — To trace a FOR loop, give the loop variable each value from start to end in turn, run the loop body once for each value, and record every pass in its own row of the trace table.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q3
FOR Count ← 1 TO 10 STEP 3 OUTPUT Count NEXT Count
Q4
FOR Num ← 1 TO 3 Total ← Total + Num NEXT Num
Q5
FOR Count ← 5 TO 1 STEP -1 OUTPUT Count NEXT Count
Section E: Spot the Error
Q1
C — Total must add each loop value, not copy it. Total ← Count overwrites Total every pass, so the trace ends with 4 instead of 10. It should be Total ← Total + Count.
Q2
B — The loop variable must run to the end value 5, giving five passes. TO 4 stops one pass early, so the trace table would have only 4 rows. It should be FOR Count ← 1 TO 5.
Q3
C — NEXT must name the same loop variable as the FOR line. The loop variable is Num, so the line should be NEXT Num.
Q4
A — A FOR loop gives its starting value to the loop variable with the assignment arrow ←, not =. The line should be FOR Count ← 1 TO 3.
Q5
D — Cambridge pseudocode ends a FOR loop with NEXT followed by the loop variable, not ENDFOR. The line should be NEXT Num.