REPEAT ... UNTIL loop (7a)

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

REPEAT ... UNTIL loop (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When does a REPEAT ... UNTIL loop test its condition?
A Before the statements run
B After the statements have run
2. What is the minimum number of times the statements in a REPEAT ... UNTIL loop run?
A Zero times
B At least once
3. When does a REPEAT ... UNTIL loop stop?
A When the condition is TRUE
B When the condition is FALSE
4. Which loop is guaranteed to run its body at least once, even if the condition is already met at the start?
A REPEAT ... UNTIL
B WHILE ... DO ... ENDWHILE
5. Which is the correct way to ask for a password until it matches 'Secret1'?
A REPEAT INPUT Pass UNTIL Pass = "Secret1"
B REPEAT INPUT Pass UNTIL Pass <> "Secret1"

Match the Pairs

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

Left columnRight column
1REPEAT INPUT Password UNTIL Password = "secret" AA REPEAT loop must finish with UNTIL and a condition. ENDREPEAT does not exist in Cambridge pseudocode.
2UNTIL Count = 10 BREPEAT OUTPUT "Hello" UNTIL TRUE
3A REPEAT loop always runs its statements at least once, because the condition is tested after them CThe condition is the wrong way round. The loop stops when the condition is TRUE, so this stops on a wrong password.
4A WHILE loop tests its condition first and can run zero times, but a REPEAT loop always runs once DThe condition tested after the loop body; the loop stops when Count equals 10 and repeats while it does not.
5REPEAT INPUT Password ENDREPEAT EAsks for a password at least once and keeps asking until the password typed in equals "secret".
6REPEAT INPUT Password UNTIL Password <> "secret" FWHILE Age < 0 DO INPUT Age ENDWHILE

Definitions

REPEAT ... UNTIL

Fill in the Blanks

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

1.
______ INPUT Password UNTIL Password = "Secret1"
2.
REPEAT INPUT Guess _____ Guess = 7
3.
REPEAT INPUT Number UNTIL Number _ 0
4.
REPEAT Count ← Count + 1 UNTIL _____ = 5
5.
REPEAT _____ Password UNTIL Password = "Secret1"

Spot the Error

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

Question 1
A REPEAT
B OUTPUT Password
C UNTIL Password = "Secret1"
Question 2
A REPEAT
B OUTPUT "Enter PIN"
C INPUT PIN
D UNTIL PIN <> 1234
Question 3
A WHILE
B INPUT Guess
C UNTIL Guess = Answer
Question 4
A Count ← 0
B REPEAT
C Count ← Count
D UNTIL Count = 5
Question 5
A Total ← 0
B REPEAT
C INPUT Num
D Total = Total + Num
E UNTIL Num = 0

Answer Key

REPEAT ... UNTIL loop (7a)
Section A: Quick Questions
Q1
B — After the statements have run
Q2
B — At least once
Q3
A — When the condition is TRUE
Q4
A — REPEAT ... UNTIL
Q5
A — REPEAT INPUT Pass UNTIL Pass = "Secret1"
Section B: Match the Pairs
Q1
REPEAT INPUT Password UNTIL Password = "secret" → Asks for a password at least once and keeps asking until the password typed in equals "secret".
Q2
UNTIL Count = 10 → The condition tested after the loop body; the loop stops when Count equals 10 and repeats while it does not.
Q3
A REPEAT loop always runs its statements at least once, because the condition is tested after them → REPEAT OUTPUT "Hello" UNTIL TRUE
Q4
A WHILE loop tests its condition first and can run zero times, but a REPEAT loop always runs once → WHILE Age < 0 DO INPUT Age ENDWHILE
Q5
REPEAT INPUT Password ENDREPEAT → A REPEAT loop must finish with UNTIL and a condition. ENDREPEAT does not exist in Cambridge pseudocode.
Q6
REPEAT INPUT Password UNTIL Password <> "secret" → The condition is the wrong way round. The loop stops when the condition is TRUE, so this stops on a wrong password.
Section C: Definitions
Q1
REPEAT ... UNTIL — A post-condition loop: its statements run at least once, then the condition is tested after them. The loop stops when the condition is TRUE.
Section D: Fill in the Blanks
Q1
REPEAT INPUT Password UNTIL Password = "Secret1"
Q2
REPEAT INPUT Guess UNTIL Guess = 7
Q3
REPEAT INPUT Number UNTIL Number > 0
Q4
REPEAT Count ← Count + 1 UNTIL Count = 5
Q5
REPEAT INPUT Password UNTIL Password = "Secret1"
Section E: Spot the Error
Q1
B — The loop needs to read a new value each time round. OUTPUT only displays Password, so the user is never asked and the condition keeps testing the same value. It should be INPUT Password.
Q2
D — A REPEAT loop stops when the condition is TRUE. With <> the loop would stop on a wrong PIN and keep asking for the right one. It should be UNTIL PIN = 1234.
Q3
A — A loop that ends with UNTIL must begin with REPEAT. WHILE belongs with DO and ENDWHILE and tests its condition first. Line 1 should be REPEAT.
Q4
C — Count never changes, so Count = 5 never becomes TRUE and the loop runs forever. The counter must go up by 1 each time: Count ← Count + 1.
Q5
D — In Cambridge pseudocode, assignment uses the arrow ←. The = sign is a comparison. It should be Total ← Total + Num.