WHILE versus REPEAT

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

WHILE versus REPEAT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student wants a loop whose body might never run at all. Which loop should they use?
A REPEAT ... UNTIL
B WHILE ... DO ... ENDWHILE
2. Which statement about REPEAT ... UNTIL is correct?
A The body always runs at least once
B The body may run zero times
3. Which describes when each loop keeps going?
A WHILE loops while the condition is TRUE; REPEAT loops until the condition is TRUE
B WHILE stops when the condition is TRUE; REPEAT loops while the condition is TRUE
4. A loop must repeat until Number >= 1 (to keep asking for a number). Rewrite as WHILE: which condition is correct?
A WHILE Number >= 1 DO
B WHILE Number < 1 DO
5. Number is 5 before this loop starts. How many times does the body run? WHILE Number < 3 DO OUTPUT Number Number ← Number + 1 ENDWHILE
A 0 times
B 1 time

Match the Pairs

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

Left columnRight column
1WHILE Count < 5 DO Count ← Count + 1 ENDWHILE ARuns the body first, then stops as soon as the condition is TRUE, so the body runs at least once
2REPEAT Count ← Count + 1 UNTIL Count >= 5 BTotal ← 10 WHILE Total < 5 DO OUTPUT Total ENDWHILE
3The same task written both ways needs the condition reversed: WHILE continues on TRUE, REPEAT stops on TRUE CWHILE Num <= 0 DO INPUT Num ENDWHILE REPEAT INPUT Num UNTIL Num > 0
4Zero-iteration case: if the condition is already false at the start, a WHILE loop body never runs DWHILE continues on TRUE, so with the condition copied from an UNTIL line it exits straight away or never runs. It should be WHILE Count < 10 DO.
5REPEAT INPUT Password UNTIL Password <> "abc" EREPEAT stops when the condition is TRUE, so this stops on a wrong password. The condition was not reversed: it should be UNTIL Password = "abc".
6WHILE Count = 10 DO Count ← Count + 1 ENDWHILE FTests the condition first and keeps looping while it is TRUE, so the body may run zero times

Definitions

WHILE vs REPEAT

Fill in the Blanks

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

1.
WHILE Num < 10 DO Num ← Num + 1 ________
2.
REPEAT Num ← Num + 1 UNTIL Num __ 10
3.
WHILE _____ DO OUTPUT "Hi" ENDWHILE
4.
______ OUTPUT "Hi" UNTIL TRUE
5.
REPEAT INPUT Age _____ Age >= 0

Spot the Error

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

Question 1
A Count ← 1
B REPEAT
C Count ← Count + 1
D UNTIL Count <= 3
Question 2
A Number ← 1
B WHILE Number < 20 DO
C Number ← Number * 2
D UNTIL Number >= 20
Question 3
A Count ← 0
B WHILE Count < 5 DO
C Count ← Count + 1
D UNTIL Count = 5
Question 4
A Total ← 0
B WHILE Total < 10 DO
C Total ← Total - 2
D ENDWHILE
Question 5
A INPUT Guess
B WHILE Guess = 7 DO
C INPUT Guess
D ENDWHILE

Answer Key

WHILE versus REPEAT
Section A: Quick Questions
Q1
B — WHILE ... DO ... ENDWHILE
Q2
A — The body always runs at least once
Q3
A — WHILE loops while the condition is TRUE; REPEAT loops until the condition is TRUE
Q4
B — WHILE Number < 1 DO
Q5
A — 0 times
Section B: Match the Pairs
Q1
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE → Tests the condition first and keeps looping while it is TRUE, so the body may run zero times
Q2
REPEAT Count ← Count + 1 UNTIL Count >= 5 → Runs the body first, then stops as soon as the condition is TRUE, so the body runs at least once
Q3
The same task written both ways needs the condition reversed: WHILE continues on TRUE, REPEAT stops on TRUE → WHILE Num <= 0 DO INPUT Num ENDWHILE REPEAT INPUT Num UNTIL Num > 0
Q4
Zero-iteration case: if the condition is already false at the start, a WHILE loop body never runs → Total ← 10 WHILE Total < 5 DO OUTPUT Total ENDWHILE
Q5
REPEAT INPUT Password UNTIL Password <> "abc" → REPEAT stops when the condition is TRUE, so this stops on a wrong password. The condition was not reversed: it should be UNTIL Password = "abc".
Q6
WHILE Count = 10 DO Count ← Count + 1 ENDWHILE → WHILE continues on TRUE, so with the condition copied from an UNTIL line it exits straight away or never runs. It should be WHILE Count < 10 DO.
Section C: Definitions
Q1
WHILE vs REPEAT — WHILE tests first, so its body may never run, and it loops while the condition is TRUE. REPEAT tests after the body, so it runs at least once, and it stops when the condition is TRUE.
Section D: Fill in the Blanks
Q1
WHILE Num < 10 DO Num ← Num + 1 ENDWHILE
Q2
REPEAT Num ← Num + 1 UNTIL Num >= 10
Q3
WHILE FALSE DO OUTPUT "Hi" ENDWHILE
Q4
REPEAT OUTPUT "Hi" UNTIL TRUE
Q5
REPEAT INPUT Age UNTIL Age >= 0
Section E: Spot the Error
Q1
D — REPEAT stops when the condition is TRUE. Count <= 3 is a WHILE-style 'keep going' condition, so the loop would stop after one pass. It should be UNTIL Count > 3.
Q2
D — A WHILE loop must end with ENDWHILE. UNTIL belongs to a REPEAT loop. Line 4 should be ENDWHILE.
Q3
B — This loop ends with UNTIL, so it must start with REPEAT. WHILE ... DO needs ENDWHILE at the end. Line 2 should be REPEAT.
Q4
C — The WHILE loop continues while Total < 10. Subtracting 2 makes Total go down, so the condition stays TRUE forever. It should be Total ← Total + 2.
Q5
B — WHILE keeps looping while the condition is TRUE. To keep asking until the guess is right, it must loop while Guess <> 7. With = the loop stops on a wrong guess and runs on a right one.