Converting between WHILE and REPEAT…UNTIL

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

Converting between WHILE and REPEAT…UNTIL

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A WHILE loop runs WHILE X < 10. Which UNTIL condition does the same job in a REPEAT...UNTIL loop?
A UNTIL X >= 10
B UNTIL X < 10
2. Which statement is true about converting WHILE Count <= 5 DO into REPEAT...UNTIL?
A Use UNTIL Count > 5
B Use UNTIL Count <= 5
3. Which loop always runs its body at least once?
A WHILE...DO...ENDWHILE
B REPEAT...UNTIL
4. X is 15 before the loop. WHILE X < 10 DO ... ENDWHILE starts. What happens?
A The body is skipped and runs 0 times
B The body runs once, then the test is made
5. X is 15 before the loop. REPEAT ... UNTIL X >= 10 starts. What happens?
A The body is skipped and runs 0 times
B The body runs once, then the loop stops

Match the Pairs

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

Left columnRight column
1WHILE X < 10 DO ... ENDWHILE becomes REPEAT ... UNTIL X >= 10 AUNTIL Count = 5
2X <- 20 REPEAT X <- X + 1 UNTIL X >= 10 BThe REPEAT...UNTIL X >= 10 condition was copied into WHILE without being reversed, so the loop will not run when X starts below 10. It should be WHILE X < 10.
3A WHILE loop that runs while Count <> 5 is converted to REPEAT...UNTIL by reversing the condition to equal CThe body runs once even though the condition is already TRUE at the start, so X ends as 21
4To copy a WHILE loop's behaviour with REPEAT...UNTIL when the condition may start FALSE, wrap the REPEAT loop in an IF with the original condition DIF X < 10 THEN REPEAT X <- X + 1 UNTIL X >= 10 ENDIF
5REPEAT X <- X + 1 UNTIL X < 10 EConverting a WHILE loop to REPEAT...UNTIL means reversing the condition: the loop now stops when X >= 10 is TRUE
6WHILE X >= 10 DO X <- X + 1 ENDWHILE FThe condition was copied from WHILE X < 10 and not reversed, so UNTIL X < 10 is the wrong way round. It should be UNTIL X >= 10.

Definitions

UNTIL X >= 10

Fill in the Blanks

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

1.
REPEAT X ← X + 1 UNTIL _______
2.
_____ X < 10 DO X ← X + 1 ENDWHILE
3.
WHILE Count __ 5 DO Count ← Count + 1 ENDWHILE
4.
REPEAT INPUT Num UNTIL Num _ 0
5.
______ OUTPUT X UNTIL X >= 10

Spot the Error

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

Question 1
A REPEAT
B X <- X + 1
C UNTIL X < 10
Question 2
A X <- 1
B WHILE X >= 10 DO
C X <- X + 1
D ENDWHILE
Question 3
A REPEAT
B OUTPUT X
C X <- X + 1
D UNTIL X <= 5
Question 4
A X <- 20
B WHILE X < 10 THEN
C X <- X + 1
D ENDWHILE
Question 5
A Count = 1
B WHILE Count <= 5 DO
C Count <- Count + 1
D ENDWHILE

Answer Key

Converting between WHILE and REPEAT…UNTIL
Section A: Quick Questions
Q1
A — UNTIL X >= 10
Q2
A — Use UNTIL Count > 5
Q3
B — REPEAT...UNTIL
Q4
A — The body is skipped and runs 0 times
Q5
B — The body runs once, then the loop stops
Section B: Match the Pairs
Q1
WHILE X < 10 DO ... ENDWHILE becomes REPEAT ... UNTIL X >= 10 → Converting a WHILE loop to REPEAT...UNTIL means reversing the condition: the loop now stops when X >= 10 is TRUE
Q2
X <- 20 REPEAT X <- X + 1 UNTIL X >= 10 → The body runs once even though the condition is already TRUE at the start, so X ends as 21
Q3
A WHILE loop that runs while Count <> 5 is converted to REPEAT...UNTIL by reversing the condition to equal → UNTIL Count = 5
Q4
To copy a WHILE loop's behaviour with REPEAT...UNTIL when the condition may start FALSE, wrap the REPEAT loop in an IF with the original condition → IF X < 10 THEN REPEAT X <- X + 1 UNTIL X >= 10 ENDIF
Q5
REPEAT X <- X + 1 UNTIL X < 10 → The condition was copied from WHILE X < 10 and not reversed, so UNTIL X < 10 is the wrong way round. It should be UNTIL X >= 10.
Q6
WHILE X >= 10 DO X <- X + 1 ENDWHILE → The REPEAT...UNTIL X >= 10 condition was copied into WHILE without being reversed, so the loop will not run when X starts below 10. It should be WHILE X < 10.
Section C: Definitions
Q1
UNTIL X >= 10 — Reverses WHILE X < 10. WHILE loops while TRUE, UNTIL stops when TRUE, so flip the condition. REPEAT also runs its body once before the first test.
Section D: Fill in the Blanks
Q1
REPEAT X ← X + 1 UNTIL X >= 10
Q2
WHILE X < 10 DO X ← X + 1 ENDWHILE
Q3
WHILE Count <> 5 DO Count ← Count + 1 ENDWHILE
Q4
REPEAT INPUT Num UNTIL Num > 0
Q5
REPEAT OUTPUT X UNTIL X >= 10
Section E: Spot the Error
Q1
C — This loop was converted from WHILE X < 10. REPEAT...UNTIL stops when its condition is TRUE, so the condition must be reversed to UNTIL X >= 10. Copying X < 10 unchanged would stop the loop at the wrong time.
Q2
B — This loop was converted from UNTIL X >= 10. WHILE keeps looping while its condition is TRUE, so the condition must be reversed to WHILE X < 10 DO. As written, the loop would never run.
Q3
D — The original WHILE loop used X <= 5. UNTIL stops when the condition is TRUE, so the reverse of <= is >. It should be UNTIL X > 5.
Q4
B — A WHILE loop header ends with DO, not THEN (THEN belongs to IF). It should be WHILE X < 10 DO. Because X starts at 20, the condition is FALSE at the first test and the body never runs.
Q5
A — Cambridge pseudocode uses <- for assignment. The = sign is a comparison. It should be Count <- 1, which sets the starting value before the loop.