WHILE ... DO ... ENDWHILE loop (7a)

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

WHILE ... DO ... ENDWHILE loop (7a)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When does a WHILE loop test its condition?
A After the statements inside the loop run
B Before the statements inside the loop run
2. Count ← 10 WHILE Count < 5 DO OUTPUT Count ENDWHILE What happens when this runs?
A The loop body runs once
B The loop body never runs
3. A WHILE loop repeats its statements for as long as the condition is...
A TRUE
B FALSE
4. Which keyword marks the end of a WHILE loop?
A ENDWHILE
B NEXT
5. Num ← 1 WHILE Num <= 5 DO OUTPUT Num ENDWHILE What is wrong with this loop?
A Num never changes, so the loop never ends
B The loop will never start

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 AWHILE Count < 5 DO Count ← Count + 1 ENDWHILE
2ENDWHILE BThe WHILE loop is closed with NEXT, which belongs to FOR loops. It must end with ENDWHILE.
3A WHILE loop tests its condition before the body, so if the condition is FALSE at the start the loop never runs CNumber ← 10 WHILE Number < 5 DO OUTPUT Number ENDWHILE
4A variable in the condition must be changed inside the loop body so the condition eventually becomes FALSE and the loop ends DMarks the end of the statements that belong to the WHILE loop and sends control back to test the condition again
5Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE ECount is never changed inside the loop, so the condition stays TRUE forever and the loop never ends. Add Count ← Count + 1 before ENDWHILE.
6WHILE Count <= 5 DO Count ← Count + 1 NEXT FTests the condition first and repeats the loop body only while Count is less than or equal to 5

Definitions

WHILE condition DO ... ENDWHILE

Fill in the Blanks

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

1.
_____ Count < 5 DO Count ← Count + 1 ENDWHILE
2.
WHILE Count < 5 DO Count ← Count + 1 ________
3.
WHILE Total < 100 __ Total ← Total + 10 ENDWHILE
4.
WHILE Count < 5 DO Count ← Count _ 1 ENDWHILE
5.
WHILE Password __ "abc" DO INPUT Password ENDWHILE

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 While Lives > 0 DO
B OUTPUT "Keep going"
C Lives ← Lives - 1
D ENDWHILE
Question 3
A Num ← 10
B WHILE Num > 0 DO
C Num ← Num + 2
D OUTPUT Num
E ENDWHILE
Question 4
A Score ← 0
B WHILE Score > 10 DO
C Score ← Score + 2
D OUTPUT Score
E ENDWHILE
Question 5
A Count = 1
B WHILE Count <= 4 DO
C OUTPUT Count * 2
D Count ← Count + 1
E ENDWHILE

Answer Key

WHILE ... DO ... ENDWHILE loop (7a)
Section A: Quick Questions
Q1
B — Before the statements inside the loop run
Q2
B — The loop body never runs
Q3
A — TRUE
Q4
A — ENDWHILE
Q5
A — Num never changes, so the loop never ends
Section B: Match the Pairs
Q1
WHILE Count <= 5 DO → Tests the condition first and repeats the loop body only while Count is less than or equal to 5
Q2
ENDWHILE → Marks the end of the statements that belong to the WHILE loop and sends control back to test the condition again
Q3
A WHILE loop tests its condition before the body, so if the condition is FALSE at the start the loop never runs → Number ← 10 WHILE Number < 5 DO OUTPUT Number ENDWHILE
Q4
A variable in the condition must be changed inside the loop body so the condition eventually becomes FALSE and the loop ends → WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q5
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Count is never changed inside the loop, so the condition stays TRUE forever and the loop never ends. Add Count ← Count + 1 before ENDWHILE.
Q6
WHILE Count <= 5 DO Count ← Count + 1 NEXT → The WHILE loop is closed with NEXT, which belongs to FOR loops. It must end with ENDWHILE.
Section C: Definitions
Q1
WHILE condition DO ... ENDWHILE — A loop that tests its condition first, then repeats its statements while the condition is TRUE. If it is FALSE at the start, the loop never runs. ENDWHILE marks the end of the loop.
Section D: Fill in the Blanks
Q1
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q2
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q3
WHILE Total < 100 DO Total ← Total + 10 ENDWHILE
Q4
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q5
WHILE Password <> "abc" DO INPUT Password ENDWHILE
Section E: Spot the Error
Q1
B — A WHILE condition must compare values. ← is assignment, not a comparison. It should be WHILE Count <= 5 DO.
Q2
A — Cambridge pseudocode keywords are written in capital letters. It should be WHILE Lives > 0 DO.
Q3
C — Num goes up, so it moves away from 0 and the condition Num > 0 never becomes FALSE. The loop never ends. It should be Num ← Num - 2.
Q4
B — The condition is tested first. Score starts at 0, so Score > 10 is FALSE straight away and the loop never runs. It should be WHILE Score < 10 DO.
Q5
A — Giving a variable a starting value needs the assignment arrow. = is used to compare values. It should be Count ← 1.