WHILE…DO…ENDWHILE pre-condition loop

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

WHILE…DO…ENDWHILE pre-condition loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When is the condition of a WHILE loop tested?
A Before each repeat of the body
B After each repeat of the body
2. Which keyword closes a WHILE loop in Cambridge pseudocode?
A NEXT
B ENDWHILE
3. Number <- 0 WHILE Number <> 0 DO OUTPUT Number ENDWHILE How many times does the body run?
A Once
B Zero times
4. The condition in WHILE Number <> 0 DO is TRUE. What happens?
A The loop keeps going
B The loop stops
5. Which WHILE loop is written correctly?
A WHILE Number <> 0 DO INPUT Number ENDWHILE
B WHILE Number <> 0 INPUT Number NEXT

Match the Pairs

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

Left columnRight column
1WHILE Number <> 0 DO AStart of a pre-condition loop: the condition is tested before each repeat, and the loop keeps going while Number is not 0
2ENDWHILE BCount never changes inside the body, so the condition stays TRUE forever and the loop never ends
3If the condition is FALSE the first time it is tested, the loop body never runs (zero times) CCloses the WHILE loop; the statements between WHILE and ENDWHILE are the body that repeats
4The body must change a variable used in the condition so the loop can end DNumber <- 0 WHILE Number <> 0 DO OUTPUT Number ENDWHILE
5WHILE Number <> 0 DO OUTPUT Number INPUT Number END EThe loop is closed with END instead of ENDWHILE, so the pseudocode is not valid
6Count <- 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE FWHILE Number <> 0 DO INPUT Number ENDWHILE

Definitions

WHILE Number <> 0 DO

Fill in the Blanks

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

1.
_____ Number <> 0 DO INPUT Number ENDWHILE
2.
WHILE Number <> 0 DO INPUT Number ________
3.
WHILE Number __ 0 DO INPUT Number ENDWHILE
4.
WHILE Number <> 0 __ INPUT Number ENDWHILE
5.
WHILE Count < 5 DO Count <- _____ + 1 ENDWHILE

Spot the Error

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

Question 1
A Total <- 0
B INPUT Number
C WHILE Number <> 0 THEN
D Total <- Total + Number
E INPUT Number
F ENDWHILE
Question 2
A Count <- 1
B WHILE Count <= 5 DO
C Count <- Count - 1
D OUTPUT "Hello"
E ENDWHILE
Question 3
A INPUT Number
B WHILE Number = 0 DO
C OUTPUT Number * 2
D INPUT Number
E ENDWHILE
Question 4
A Number = 10
B WHILE Number > 0 DO
C OUTPUT Number
D Number <- Number - 3
E ENDWHILE
Question 5
A Total <- 0
B WHILE Total > 100 DO
C INPUT Score
D Total <- Total + Score
E ENDWHILE

Answer Key

WHILE…DO…ENDWHILE pre-condition loop
Section A: Quick Questions
Q1
A — Before each repeat of the body
Q2
B — ENDWHILE
Q3
B — Zero times
Q4
A — The loop keeps going
Q5
A — WHILE Number <> 0 DO INPUT Number ENDWHILE
Section B: Match the Pairs
Q1
WHILE Number <> 0 DO → Start of a pre-condition loop: the condition is tested before each repeat, and the loop keeps going while Number is not 0
Q2
ENDWHILE → Closes the WHILE loop; the statements between WHILE and ENDWHILE are the body that repeats
Q3
If the condition is FALSE the first time it is tested, the loop body never runs (zero times) → Number <- 0 WHILE Number <> 0 DO OUTPUT Number ENDWHILE
Q4
The body must change a variable used in the condition so the loop can end → WHILE Number <> 0 DO INPUT Number ENDWHILE
Q5
WHILE Number <> 0 DO OUTPUT Number INPUT Number END → The loop is closed with END instead of ENDWHILE, so the pseudocode is not valid
Q6
Count <- 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Count never changes inside the body, so the condition stays TRUE forever and the loop never ends
Section C: Definitions
Q1
WHILE Number <> 0 DO — A pre-condition loop: the condition is tested before every repeat, so the body may run zero times. The loop keeps going while the condition is TRUE and is closed by ENDWHILE.
Section D: Fill in the Blanks
Q1
WHILE Number <> 0 DO INPUT Number ENDWHILE
Q2
WHILE Number <> 0 DO INPUT Number ENDWHILE
Q3
WHILE Number <> 0 DO INPUT Number ENDWHILE
Q4
WHILE Number <> 0 DO INPUT Number ENDWHILE
Q5
WHILE Count < 5 DO Count <- Count + 1 ENDWHILE
Section E: Spot the Error
Q1
C — A WHILE loop header ends with DO, not THEN. THEN belongs to IF statements. It should be WHILE Number <> 0 DO.
Q2
C — Count goes down, so Count <= 5 stays TRUE forever and the loop never ends. The body must move Count towards making the condition FALSE: Count <- Count + 1.
Q3
B — The loop should keep going while Number is NOT the rogue value 0. With = 0 the condition is FALSE for most inputs, so the body is skipped. It should be WHILE Number <> 0 DO.
Q4
A — In Cambridge pseudocode, assignment uses the arrow <-. The = sign is a comparison. It should be Number <- 10.
Q5
B — Total starts at 0, so Total > 100 is FALSE at the first test and the body never runs. The loop should continue while Total < 100.