WHILE…DO…ENDWHILE loop (7d)

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

WHILE…DO…ENDWHILE loop (7d)

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 Before each run of the loop body, including the first
B After each run of the loop body
2. Num ← 10 WHILE Num < 5 DO OUTPUT Num ENDWHILE How many times does the loop body run?
A 0 times
B 1 time
3. A WHILE loop repeats its body while the condition is...
A FALSE
B TRUE
4. Count ← 1 WHILE Count <= 3 DO OUTPUT Count ENDWHILE What is wrong with this loop?
A OUTPUT cannot be used inside a WHILE loop
B Count is never changed, so the loop never ends
5. Which line stops this loop from running forever? WHILE Total < 100 DO INPUT Score ... ENDWHILE
A Total ← Total + Score
B OUTPUT Total

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 OUTPUT Count Count ← Count + 1 ENDWHILE
2ENDWHILE BTests the condition before each repeat and runs the loop body only while Count < 5 is TRUE
3The body of a WHILE loop may run zero times because the condition is tested first CMarks the end of the loop body, then the program jumps back to test the WHILE condition again
4The variable in the condition must be updated inside the loop so the condition can become FALSE DThe keyword DO is missing from the WHILE line. It should be WHILE Count < 5 DO.
5Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE ECount is never changed inside the loop, so Count <= 5 stays TRUE forever and the loop is infinite. Add Count ← Count + 1 in the body.
6WHILE Count < 5 Count ← Count + 1 ENDWHILE FCount ← 10 WHILE Count < 5 DO OUTPUT Count ENDWHILE

Definitions

WHILE condition DO

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 Count < 3 DO Count ← Count _ 1 ENDWHILE
4.
WHILE Num > 0 __ Num ← Num - 1 ENDWHILE
5.
WHILE Num > 0 DO ___ ← Num - 1 ENDWHILE

Spot the Error

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

Question 1
A WHILE Count < 5
B OUTPUT Count
C Count ← Count + 1
D ENDWHILE
Question 2
A Total ← 0
B WHILE Total < 100 DO
C Total ← Total + 10
D NEXT
E OUTPUT Total
Question 3
A Num ← 1
B WHILE Num <= 5 DO
C Num ← Num - 1
D OUTPUT Num
E ENDWHILE
Question 4
A INPUT Guess
B WHILE Guess <> 7 DO
C OUTPUT Guess
D ENDWHILE
E OUTPUT "Correct"
Question 5
A Total ← 0
B Num ← 1
C WHILE Num <= 4 THEN
D Total ← Total + Num
E Num ← Num + 1
F ENDWHILE

Answer Key

WHILE…DO…ENDWHILE loop (7d)
Section A: Quick Questions
Q1
A — Before each run of the loop body, including the first
Q2
A — 0 times
Q3
B — TRUE
Q4
B — Count is never changed, so the loop never ends
Q5
A — Total ← Total + Score
Section B: Match the Pairs
Q1
WHILE Count < 5 DO → Tests the condition before each repeat and runs the loop body only while Count < 5 is TRUE
Q2
ENDWHILE → Marks the end of the loop body, then the program jumps back to test the WHILE condition again
Q3
The body of a WHILE loop may run zero times because the condition is tested first → Count ← 10 WHILE Count < 5 DO OUTPUT Count ENDWHILE
Q4
The variable in the condition must be updated inside the loop so the condition can become FALSE → WHILE Count < 5 DO OUTPUT Count Count ← Count + 1 ENDWHILE
Q5
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Count is never changed inside the loop, so Count <= 5 stays TRUE forever and the loop is infinite. Add Count ← Count + 1 in the body.
Q6
WHILE Count < 5 Count ← Count + 1 ENDWHILE → The keyword DO is missing from the WHILE line. It should be WHILE Count < 5 DO.
Section C: Definitions
Q1
WHILE condition DO — A loop that tests the condition first and repeats its body while it is TRUE. The body may run zero times, and it must change the variable or the loop never ends.
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 Count < 3 DO Count ← Count + 1 ENDWHILE
Q4
WHILE Num > 0 DO Num ← Num - 1 ENDWHILE
Q5
WHILE Num > 0 DO Num ← Num - 1 ENDWHILE
Section E: Spot the Error
Q1
A — A WHILE loop header must end with DO. It should be WHILE Count < 5 DO.
Q2
D — A WHILE loop is closed with ENDWHILE. NEXT belongs to FOR loops.
Q3
C — Num goes down, so Num <= 5 is always TRUE and the loop never ends (infinite loop). The loop must add 1: Num ← Num + 1.
Q4
C — The loop body must change Guess, otherwise the condition never becomes FALSE and the loop is infinite. It should be INPUT Guess.
Q5
C — WHILE loops use DO, not THEN. THEN is used with IF. It should be WHILE Num <= 4 DO.