Pseudocode WHILE ... DO ... ENDWHILE and REPEAT ... UNTIL

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

Pseudocode WHILE ... DO ... ENDWHILE and REPEAT ... UNTIL

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop tests its condition BEFORE running the body for the first time?
A WHILE ... DO ... ENDWHILE
B REPEAT ... UNTIL
2. Which loop always runs its body at least once?
A REPEAT ... UNTIL
B WHILE ... DO ... ENDWHILE
3. Count ← 10 comes before: WHILE Count < 5 DO ... ENDWHILE. How many times does the body run?
A 0 times
B 1 time
4. Count ← 10 comes before: REPEAT ... UNTIL Count > 5. How many times does the body run?
A 0 times
B 1 time
5. Which loop ends when its condition becomes TRUE?
A WHILE ... DO ... ENDWHILE
B REPEAT ... UNTIL

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 AREPEAT must end with UNTIL, not WHILE. UNTIL stops when the condition is true.
2REPEAT INPUT Password UNTIL Password = "cat" BREPEAT INPUT Number UNTIL Number > 0
3A loop that must ask for a number at least once, then check it afterwards CWHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
4A loop whose condition is tested first, so it might never run DTests the condition before each repeat, so the loop body may run 0 times
5WHILE Total < 100 DO OUTPUT Total ENDWHILE EThe condition can never change because Total is not updated inside the loop. This makes an infinite loop.
6REPEAT Count ← Count + 1 WHILE Count = 10 FRuns the body first and tests after, so it always runs at least once

Definitions

WHILE ... DO ... ENDWHILE

Fill in the Blanks

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

1.
WHILE Count < 5 DO Count ← Count + 1 ________
2.
REPEAT INPUT Guess _____ Guess = 7
3.
WHILE Count < 5 __ Count ← Count + 1 ENDWHILE
4.
WHILE Lives > 0 DO Lives ← Lives _ 1 ENDWHILE
5.
______ INPUT Password UNTIL Password = "cloodit"

Spot the Error

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

Question 1
A Count ← 1
B WHILE Count <= 5 DO
C OUTPUT Count
D Count ← Count - 1
E ENDWHILE
Question 2
A Total ← 0
B REPEAT DO
C INPUT Num
D Total ← Total + Num
E UNTIL Total >= 100
Question 3
A INPUT Pass
B WHILE Pass <> 1234 DO
C OUTPUT Pass
D ENDWHILE
Question 4
A WHILE Lives > 0
B OUTPUT Lives
C Lives ← Lives - 1
D ENDWHILE
Question 5
A Num ← 0
B REPEAT
C Num ← Num + 1
D ENDREPEAT
E OUTPUT Num

Answer Key

Pseudocode WHILE ... DO ... ENDWHILE and REPEAT ... UNTIL
Section A: Quick Questions
Q1
A — WHILE ... DO ... ENDWHILE
Q2
A — REPEAT ... UNTIL
Q3
A — 0 times
Q4
B — 1 time
Q5
B — REPEAT ... UNTIL
Section B: Match the Pairs
Q1
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE → Tests the condition before each repeat, so the loop body may run 0 times
Q2
REPEAT INPUT Password UNTIL Password = "cat" → Runs the body first and tests after, so it always runs at least once
Q3
A loop that must ask for a number at least once, then check it afterwards → REPEAT INPUT Number UNTIL Number > 0
Q4
A loop whose condition is tested first, so it might never run → WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Q5
WHILE Total < 100 DO OUTPUT Total ENDWHILE → The condition can never change because Total is not updated inside the loop. This makes an infinite loop.
Q6
REPEAT Count ← Count + 1 WHILE Count = 10 → REPEAT must end with UNTIL, not WHILE. UNTIL stops when the condition is true.
Section C: Definitions
Q1
WHILE ... DO ... ENDWHILE — A loop that tests its condition before each repeat, so it may run 0 times. REPEAT ... UNTIL runs the body first and tests after, so it always runs at least once.
Section D: Fill in the Blanks
Q1
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q2
REPEAT INPUT Guess UNTIL Guess = 7
Q3
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q4
WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Q5
REPEAT INPUT Password UNTIL Password = "cloodit"
Section E: Spot the Error
Q1
D — Count goes down, so Count <= 5 is always true and the WHILE loop never ends. The body must change the variable towards ending the loop: Count ← Count + 1.
Q2
B — REPEAT ... UNTIL does not use DO. DO belongs to WHILE ... DO. The line should just be REPEAT.
Q3
C — The condition is tested before each repeat, so Pass must be able to change inside the loop. OUTPUT does not change it, so the loop never ends. It should be INPUT Pass.
Q4
A — A WHILE loop must have the keyword DO after its condition. The line should be WHILE Lives > 0 DO.
Q5
D — A REPEAT loop ends with UNTIL followed by a condition, which is tested after the body runs. There is no ENDREPEAT. The line should be UNTIL Num = 3.