Tracing WHILE and REPEAT loops

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

Tracing WHILE and REPEAT loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace of WHILE Count < 3 DO starts with Count = 5. How many times does the loop body run?
A 0 times, because the condition is tested first and is FALSE
B 1 time, because the body always runs once
2. A REPEAT loop ends with UNTIL Num > 0. The first test input is 7. How many times does the body run?
A Once, then the loop stops
B 0 times, because 7 already passes the test
3. When tracing a REPEAT ... UNTIL loop, where do you check the condition on each pass?
A After the body has run
B Before the body runs
4. REPEAT INPUT Num UNTIL Num >= 0 is traced with test inputs -3, -1, 6. How many values are read?
A 1, because the loop stops after the first input
B 3, because the loop stops only when 6 is entered
5. Trace WHILE Total < 10 DO Total ← Total + 4 ENDWHILE with Total starting at 0. What is Total when the loop ends?
A 10
B 12

Match the Pairs

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

Left columnRight column
1WHILE Total < 10 DO Total ← Total + 4 ENDWHILE AThe student ran the body without testing first. X < 5 is false at the start (8 is not less than 5), so the body never runs and X stays 8.
2REPEAT INPUT Guess UNTIL Guess = 7 BREPEAT tests after the body, so the body always runs once. N becomes 11, and then N > 5 is true, so the loop stops after one pass.
3When tracing a loop that reads input, use the next test input on each pass and stop when the sentinel value is read. CCount ← 1 WHILE Count < 5 DO OUTPUT Count ENDWHILE
4A loop whose condition never changes to end it will never stop, so the trace table keeps adding rows forever. DThe body runs first and the condition Guess = 7 is tested AFTER it, so the loop runs at least once and stops when the condition is true.
5Trace of: X ← 8 WHILE X < 5 DO X ← X + 1 ENDWHILE Trace shows X = 9 after the loop EINPUT Num WHILE Num <> -1 DO Total ← Total + Num INPUT Num ENDWHILE
6Trace of: N ← 10 REPEAT N ← N + 1 UNTIL N > 5 Trace shows the loop does not run FThe condition Total < 10 is tested BEFORE each pass of the body, so the body may run zero times.

Definitions

WHILE and REPEAT loop trace

Fill in the Blanks

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

1.
WHILE Count < 3 DO Count ← Count + 1 ________
2.
REPEAT Count ← Count + 1 _____ Count = 3
3.
Count ← 5 _____ Count < 3 DO OUTPUT Count
4.
REPEAT INPUT _____ UNTIL Value = 0
5.
WHILE Count < 3 DO Count ← _____ + 1 ENDWHILE

Spot the Error

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

Question 1
A Count ← 1
B WHILE Count <= 3
C OUTPUT Count
D Count ← Count + 1
E ENDWHILE
Question 2
A Total ← 0
B WHILE Total < 10 DO
C Total ← Total - 4
D ENDWHILE
Question 3
A Num ← 0
B WHILE Num < 20 DO
C Num ← Num * 2
D ENDWHILE
E OUTPUT Num
Question 4
A Tries ← 1
B REPEAT
C Tries ← Tries + 1
D INPUT Guess
E UNTIL Guess = 7
Question 5
A INPUT Mark
B WHILE Mark < 0 AND Mark > 100 DO
C OUTPUT "Try again"
D INPUT Mark
E ENDWHILE

Answer Key

Tracing WHILE and REPEAT loops
Section A: Quick Questions
Q1
A — 0 times, because the condition is tested first and is FALSE
Q2
A — Once, then the loop stops
Q3
A — After the body has run
Q4
B — 3, because the loop stops only when 6 is entered
Q5
B — 12
Section B: Match the Pairs
Q1
WHILE Total < 10 DO Total ← Total + 4 ENDWHILE → The condition Total < 10 is tested BEFORE each pass of the body, so the body may run zero times.
Q2
REPEAT INPUT Guess UNTIL Guess = 7 → The body runs first and the condition Guess = 7 is tested AFTER it, so the loop runs at least once and stops when the condition is true.
Q3
When tracing a loop that reads input, use the next test input on each pass and stop when the sentinel value is read. → INPUT Num WHILE Num <> -1 DO Total ← Total + Num INPUT Num ENDWHILE
Q4
A loop whose condition never changes to end it will never stop, so the trace table keeps adding rows forever. → Count ← 1 WHILE Count < 5 DO OUTPUT Count ENDWHILE
Q5
Trace of: X ← 8 WHILE X < 5 DO X ← X + 1 ENDWHILE Trace shows X = 9 after the loop → The student ran the body without testing first. X < 5 is false at the start (8 is not less than 5), so the body never runs and X stays 8.
Q6
Trace of: N ← 10 REPEAT N ← N + 1 UNTIL N > 5 Trace shows the loop does not run → REPEAT tests after the body, so the body always runs once. N becomes 11, and then N > 5 is true, so the loop stops after one pass.
Section C: Definitions
Q1
WHILE and REPEAT loop trace — Trace a loop by testing its condition each pass: WHILE tests before the body and may never run; REPEAT tests after the body so runs once at least. Stop when the loop ends.
Section D: Fill in the Blanks
Q1
WHILE Count < 3 DO Count ← Count + 1 ENDWHILE
Q2
REPEAT Count ← Count + 1 UNTIL Count = 3
Q3
Count ← 5 WHILE Count < 3 DO OUTPUT Count
Q4
REPEAT INPUT Value UNTIL Value = 0
Q5
WHILE Count < 3 DO Count ← Count + 1 ENDWHILE
Section E: Spot the Error
Q1
B — A WHILE loop header needs the keyword DO after the condition. It should be WHILE Count <= 3 DO.
Q2
C — Total goes 0, -4, -8... and never reaches 10, so the condition Total < 10 is always true and the loop never ends. It should be Total ← Total + 4.
Q3
A — If Num starts at 0, then 0 * 2 is always 0. Num never reaches 20, so the loop never ends. It should start with Num ← 1.
Q4
A — The counter must start at 0. REPEAT runs the body once before testing, so Tries becomes 1 after the first pass. Starting at 1 would count one attempt too many.
Q5
B — A mark cannot be below 0 and above 100 at the same time, so the condition is always false. The WHILE loop never runs, even for invalid marks. It should use OR.