WHILE ... DO ... ENDWHILE

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

WHILE ... DO ... ENDWHILE

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 pass through the loop
B After each pass through the loop
2. Number ← 5 WHILE Number > 9 DO Number ← Number + 1 ENDWHILE How many times does the body run?
A At least once
B Zero times
3. A WHILE loop repeats its statements while the condition is...
A FALSE
B TRUE
4. Which line correctly closes a WHILE loop?
A ENDWHILE
B NEXT
5. Number ← 20 WHILE Number > 9 DO OUTPUT Number ENDWHILE What is wrong with this loop?
A Nothing in the body changes Number, so the loop never ends
B WHILE loops cannot use the > operator

Match the Pairs

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

Left columnRight column
1WHILE Number > 9 DO AThe loop is closed with NEXT, which belongs to a FOR loop, instead of ENDWHILE
2ENDWHILE BNothing in the body changes Number, so the condition stays TRUE and the loop never ends (an infinite loop)
3A WHILE loop tests its condition first, so if the condition starts FALSE the body runs zero times CCloses the WHILE loop and sends control back to test the condition again
4Something inside the body must change the condition, or the loop never ends DTests the condition before each pass and runs the loop body only while Number is greater than 9
5WHILE Number > 9 DO Count ← Count + 1 ENDWHILE ENumber ← 5 WHILE Number > 9 DO OUTPUT Number ENDWHILE
6WHILE Number > 9 DO Number ← Number DIV 10 NEXT FWHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE

Definitions

WHILE ... DO ... ENDWHILE

Fill in the Blanks

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

1.
_____ Number > 9 DO Number ← Number DIV 10 ENDWHILE
2.
WHILE Count < 5 __ Count ← Count + 1 ENDWHILE
3.
WHILE Number > 9 DO Number ← Number DIV 10 ________
4.
WHILE Count < 5 DO OUTPUT Count _____ ← Count + 1
5.
WHILE Lives _ 0 DO Lives ← Lives - 1 ENDWHILE

Spot the Error

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

Question 1
A Count ← 0
B WHILE Number > 9 DO
C Count = Count + 1
D Number ← Number DIV 10
E ENDWHILE
Question 2
A Count ← 0
B WHILE Count < 5 THEN
C OUTPUT Count
D Count ← Count + 1
E ENDWHILE
Question 3
A Number ← 100
B WHILE Number > 9 DO
C Number ← Number * 10
D ENDWHILE
E OUTPUT Number
Question 4
A Count ← 0
B WHILE Count < 3 DO
C Count ← Count + 1
D ENDIF
E OUTPUT Count
Question 5
A Total ← 0
B WHILE Total < 50 DO
C INPUT Value
D Total ← Value
E ENDWHILE
F OUTPUT Total

Answer Key

WHILE ... DO ... ENDWHILE
Section A: Quick Questions
Q1
A — Before each pass through the loop
Q2
B — Zero times
Q3
B — TRUE
Q4
A — ENDWHILE
Q5
A — Nothing in the body changes Number, so the loop never ends
Section B: Match the Pairs
Q1
WHILE Number > 9 DO → Tests the condition before each pass and runs the loop body only while Number is greater than 9
Q2
ENDWHILE → Closes the WHILE loop and sends control back to test the condition again
Q3
A WHILE loop tests its condition first, so if the condition starts FALSE the body runs zero times → Number ← 5 WHILE Number > 9 DO OUTPUT Number ENDWHILE
Q4
Something inside the body must change the condition, or the loop never ends → WHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE
Q5
WHILE Number > 9 DO Count ← Count + 1 ENDWHILE → Nothing in the body changes Number, so the condition stays TRUE and the loop never ends (an infinite loop)
Q6
WHILE Number > 9 DO Number ← Number DIV 10 NEXT → The loop is closed with NEXT, which belongs to a FOR loop, instead of ENDWHILE
Section C: Definitions
Q1
WHILE ... DO ... ENDWHILE — A loop that tests its condition before each pass and repeats its statements while the condition is TRUE. It may run zero times, and the body must change the condition.
Section D: Fill in the Blanks
Q1
WHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE
Q2
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q3
WHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE
Q4
WHILE Count < 5 DO OUTPUT Count Count ← Count + 1
Q5
WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Section E: Spot the Error
Q1
C — = is used for comparison, not for storing a value. Assignment must use the arrow, so the line should be Count ← Count + 1.
Q2
B — A WHILE loop header ends with DO, not THEN. THEN belongs to IF statements. It should be WHILE Count < 5 DO.
Q3
C — The body must change the variable so the condition eventually becomes FALSE. Multiplying makes Number bigger, so Number > 9 stays TRUE forever. It should be Number ← Number DIV 10.
Q4
D — A WHILE loop is closed with ENDWHILE. ENDIF closes an IF statement, so line 4 should be ENDWHILE.
Q5
D — This overwrites Total each pass instead of adding to it, so the total never builds up and the loop may never end. It should be Total ← Total + Value.