REPEAT…UNTIL post-condition loop

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

REPEAT…UNTIL post-condition loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A REPEAT...UNTIL loop tests its condition at which point?
A Before the body runs
B After the body has run
2. The body of a REPEAT...UNTIL loop runs...
A At least once, even if the condition is already TRUE
B Zero times if the condition is already TRUE
3. A REPEAT...UNTIL loop keeps looping while the condition is...
A FALSE
B TRUE
4. Which line correctly ends a REPEAT loop?
A ENDREPEAT
B UNTIL Answer = "Y"
5. Which statement about loop conditions is correct?
A WHILE continues when TRUE, but REPEAT...UNTIL stops when TRUE
B WHILE and REPEAT...UNTIL both stop when the condition is TRUE

Match the Pairs

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

Left columnRight column
1REPEAT INPUT Answer UNTIL Answer = "Y" AThe loop ends as soon as this condition becomes TRUE
2UNTIL Count > 5 BAsks for Answer at least once, then keeps asking until the user types Y
3A REPEAT loop always runs its body at least once, even if the condition is already TRUE at the start CENDREPEAT does not exist. A REPEAT loop must finish with UNTIL followed by the condition.
4A REPEAT loop is closed by UNTIL and its condition, with no separate end keyword such as ENDREPEAT DREPEAT Total <- Total + 1 UNTIL Total = 10
5REPEAT INPUT Pin ENDREPEAT Pin = 1234 ENum <- 10 REPEAT OUTPUT Num UNTIL Num > 5
6REPEAT INPUT Answer UNTIL Answer <> "Y" FThe condition is the wrong way round. UNTIL stops when TRUE, so this stops on any answer except Y, instead of repeating until the user types Y.

Definitions

REPEAT UNTIL

Fill in the Blanks

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

1.
REPEAT INPUT Answer _____ Answer = "Y"
2.
______ INPUT Answer UNTIL Answer = "Y"
3.
REPEAT INPUT Answer UNTIL Answer _ "Y"
4.
REPEAT INPUT Num UNTIL Num >= 1 ___ Num <= 10
5.
REPEAT Count <- Count + 1 UNTIL _____ = 5

Spot the Error

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

Question 1
A REPEAT
B INPUT Answer
C ENDREPEAT
Question 2
A Count <- 0
B REPEAT DO
C Count <- Count + 1
D OUTPUT Count
E UNTIL Count = 5
Question 3
A REPEAT
B OUTPUT "Enter a number"
C INPUT Num
D UNTIL Num < 1 OR Num > 10
Question 4
A REPEAT
B INPUT Password
C UNTIL Password == "cs2024"
Question 5
A DO
B OUTPUT "Guess the number"
C INPUT Guess
D UNTIL Guess = Secret

Answer Key

REPEAT…UNTIL post-condition loop
Section A: Quick Questions
Q1
B — After the body has run
Q2
A — At least once, even if the condition is already TRUE
Q3
A — FALSE
Q4
B — UNTIL Answer = "Y"
Q5
A — WHILE continues when TRUE, but REPEAT...UNTIL stops when TRUE
Section B: Match the Pairs
Q1
REPEAT INPUT Answer UNTIL Answer = "Y" → Asks for Answer at least once, then keeps asking until the user types Y
Q2
UNTIL Count > 5 → The loop ends as soon as this condition becomes TRUE
Q3
A REPEAT loop always runs its body at least once, even if the condition is already TRUE at the start → Num <- 10 REPEAT OUTPUT Num UNTIL Num > 5
Q4
A REPEAT loop is closed by UNTIL and its condition, with no separate end keyword such as ENDREPEAT → REPEAT Total <- Total + 1 UNTIL Total = 10
Q5
REPEAT INPUT Pin ENDREPEAT Pin = 1234 → ENDREPEAT does not exist. A REPEAT loop must finish with UNTIL followed by the condition.
Q6
REPEAT INPUT Answer UNTIL Answer <> "Y" → The condition is the wrong way round. UNTIL stops when TRUE, so this stops on any answer except Y, instead of repeating until the user types Y.
Section C: Definitions
Q1
REPEAT UNTIL — A post-condition loop: the body runs first, then UNTIL tests the condition. It always runs at least once and stops when the condition is TRUE. There is no ENDREPEAT.
Section D: Fill in the Blanks
Q1
REPEAT INPUT Answer UNTIL Answer = "Y"
Q2
REPEAT INPUT Answer UNTIL Answer = "Y"
Q3
REPEAT INPUT Answer UNTIL Answer = "Y"
Q4
REPEAT INPUT Num UNTIL Num >= 1 AND Num <= 10
Q5
REPEAT Count <- Count + 1 UNTIL Count = 5
Section E: Spot the Error
Q1
C — A REPEAT loop is closed by the UNTIL line, which holds the condition. There is no ENDREPEAT keyword. The line should be UNTIL Answer = "Y".
Q2
B — REPEAT stands alone on its line. DO belongs to WHILE...DO, not to REPEAT...UNTIL. The line should be just REPEAT.
Q3
D — REPEAT...UNTIL stops when the condition is TRUE. This condition is TRUE for invalid numbers, so the loop would stop on bad input. It should test for a valid number: UNTIL Num >= 1 AND Num <= 10.
Q4
C — Cambridge pseudocode uses a single = to compare values, not ==. The line should be UNTIL Password = "cs2024".
Q5
A — A post-condition loop must start with the keyword REPEAT. DO is not used to begin a REPEAT...UNTIL loop. Line 1 should be REPEAT.