Choosing between FOR, WHILE and REPEAT

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

Choosing between FOR, WHILE and REPEAT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program must display a message exactly 10 times. Which loop is the best choice?
A REPEAT ... UNTIL
B FOR ... TO ... NEXT
2. A loop might need to run zero times, for example if the user has no items to enter. Which loop is the best choice?
A WHILE ... DO ... ENDWHILE
B REPEAT ... UNTIL
3. A password must be asked for at least once, then asked again until it is correct. Which loop is the best choice?
A REPEAT ... UNTIL
B FOR ... TO ... NEXT
4. Which statement is true?
A REPEAT tests its condition first; WHILE tests its condition last.
B WHILE tests its condition first; REPEAT tests its condition last.
5. Which loop is count-controlled?
A FOR Count ← 1 TO 20
B WHILE Total < 100 DO

Match the Pairs

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

Left columnRight column
1FOR Count ← 1 TO 10 AFOR Number ← 1 TO 12 OUTPUT Number * 7 NEXT Number
2WHILE Password <> "open" DO BA FOR loop is the wrong choice because the number of repeats is not known. A condition-controlled loop such as REPEAT ... UNTIL or WHILE is needed.
3A loop must run at least once, such as asking for a password before it can be checked, with the test at the end CA count-controlled loop: use it when the number of repeats is known before the loop starts
4A scenario where the number of repeats is known: output the 12 times table DA pre-condition loop: the test happens first, so the body may run zero times
5FOR Count ← 1 TO TotalPressed INPUT Key NEXT Count (used to keep asking until the user types STOP) EREPEAT INPUT Password UNTIL Password = "open"
6REPEAT OUTPUT Item Count ← Count + 1 UNTIL Count > Total (Total could be 0 and nothing should print) FREPEAT always runs at least once, so it would output an item even when Total is 0. A WHILE loop should be used because it can run zero times.

Definitions

FOR, WHILE or REPEAT

Fill in the Blanks

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

1.
___ Count ← 1 TO 10 OUTPUT Count NEXT Count
2.
_____ Stock > 0 DO Stock ← Stock - 1 ENDWHILE
3.
______ INPUT Guess UNTIL Guess = 7
4.
REPEAT INPUT Guess _____ Guess = Secret
5.
FOR Day ← 1 TO 7 OUTPUT Day ____ Day

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 1 UNTIL 5
C INPUT Mark
D Total ← Total + Mark
E NEXT Count
Question 2
A REPEAT
B INPUT Pin
C UNTIL Pin <> 1234
Question 3
A INPUT Mark
B WHILE Mark >= 0 THEN
C Total ← Total + Mark
D INPUT Mark
E ENDWHILE
Question 4
A FOR Day ← 1 TO 7
B INPUT Steps
C Total ← Total + Steps
D ENDFOR
Question 5
A WHILE Choice <> 0 DO
B OUTPUT "1 Play 0 Quit"
C INPUT Choice
D UNTIL Choice = 0

Answer Key

Choosing between FOR, WHILE and REPEAT
Section A: Quick Questions
Q1
B — FOR ... TO ... NEXT
Q2
A — WHILE ... DO ... ENDWHILE
Q3
A — REPEAT ... UNTIL
Q4
B — WHILE tests its condition first; REPEAT tests its condition last.
Q5
A — FOR Count ← 1 TO 20
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 10 → A count-controlled loop: use it when the number of repeats is known before the loop starts
Q2
WHILE Password <> "open" DO → A pre-condition loop: the test happens first, so the body may run zero times
Q3
A loop must run at least once, such as asking for a password before it can be checked, with the test at the end → REPEAT INPUT Password UNTIL Password = "open"
Q4
A scenario where the number of repeats is known: output the 12 times table → FOR Number ← 1 TO 12 OUTPUT Number * 7 NEXT Number
Q5
FOR Count ← 1 TO TotalPressed INPUT Key NEXT Count (used to keep asking until the user types STOP) → A FOR loop is the wrong choice because the number of repeats is not known. A condition-controlled loop such as REPEAT ... UNTIL or WHILE is needed.
Q6
REPEAT OUTPUT Item Count ← Count + 1 UNTIL Count > Total (Total could be 0 and nothing should print) → REPEAT always runs at least once, so it would output an item even when Total is 0. A WHILE loop should be used because it can run zero times.
Section C: Definitions
Q1
FOR, WHILE or REPEAT — Use FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT when it must run at least once. WHILE tests first; REPEAT tests last.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Q2
WHILE Stock > 0 DO Stock ← Stock - 1 ENDWHILE
Q3
REPEAT INPUT Guess UNTIL Guess = 7
Q4
REPEAT INPUT Guess UNTIL Guess = Secret
Q5
FOR Day ← 1 TO 7 OUTPUT Day NEXT Day
Section E: Spot the Error
Q1
B — A FOR loop is used when the number of repeats is known, and its range is written with TO, not UNTIL. It should be FOR Count ← 1 TO 5.
Q2
C — REPEAT...UNTIL tests at the end and stops when the condition is TRUE. The loop should stop when the PIN is correct, so it should be UNTIL Pin = 1234.
Q3
B — A WHILE loop header ends with DO, not THEN (THEN belongs to IF). It should be WHILE Mark >= 0 DO.
Q4
D — A FOR loop is closed with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Day.
Q5
A — The menu must be shown at least once, so REPEAT is the right loop. WHILE tests first, before Choice has any value, and would not match the UNTIL at the end. Line 1 should be REPEAT.