Choosing FOR, WHILE or REPEAT

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

Choosing FOR, WHILE or REPEAT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program must output the numbers 1 to 10. Which loop is the best choice?
A FOR Count ← 1 TO 10
B WHILE Count > 10 DO
2. A program keeps asking for a password until the user types the right one. Which type of loop fits?
A A condition-controlled loop (WHILE or REPEAT)
B A count-controlled loop (FOR)
3. Which statement about REPEAT...UNTIL is true?
A The condition is tested at the start, so the loop body may never run
B The condition is tested at the end, so the loop body runs at least once
4. Which statement about WHILE...DO...ENDWHILE is true?
A The condition is tested at the end, so the body always runs once
B The condition is tested at the start, so the body might never run
5. A program must process exactly 30 test marks stored in an array. Which loop should you choose?
A FOR Index ← 1 TO 30
B REPEAT ... UNTIL the user stops typing

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 needs a known number as its end value, not a condition. A condition-based repeat needs WHILE or REPEAT.
2REPEAT ... UNTIL Guess = Secret BA WHILE loop must end with ENDWHILE, not NEXT. NEXT belongs to a FOR loop.
3The condition is tested at the start, so the loop body may never run CA count-controlled loop: the number of repeats is known (10) before it starts
4The number of repeats is known: read exactly 5 marks DWHILE Password <> "open" DO
5WHILE Total < 100 DO INPUT Num Total ← Total + Num NEXT EA condition-controlled loop that tests at the end, so the body always runs at least once
6FOR Count ← 1 TO Answer <> 5 FFOR Index ← 1 TO 5

Definitions

FOR, WHILE, REPEAT

Fill in the Blanks

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

1.
___ Count ← 1 TO 5 OUTPUT Count NEXT Count
2.
_____ Number < 0 DO INPUT Number ENDWHILE
3.
REPEAT INPUT Guess _____ Guess = 7
4.
WHILE Lives > 0 DO Lives ← Lives - 1 ________
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 FOR Student ← 1 TO 30
B INPUT Mark
C OUTPUT Mark
D ENDFOR
Question 2
A INPUT Password
B WHILE Password <> "open" DO
C INPUT Password
D NEXT Password
Question 3
A Guess ← 0
B WHILE
C INPUT Guess
D UNTIL Guess = 7
Question 4
A Total ← 0
B INPUT Mark
C WHILE Mark = -1 DO
D Total ← Total + Mark
E INPUT Mark
F ENDWHILE
Question 5
A Total ← 0
B FOR Num = 1 TO 10
C Total ← Total + Num
D NEXT Num

Answer Key

Choosing FOR, WHILE or REPEAT
Section A: Quick Questions
Q1
A — FOR Count ← 1 TO 10
Q2
A — A condition-controlled loop (WHILE or REPEAT)
Q3
B — The condition is tested at the end, so the loop body runs at least once
Q4
B — The condition is tested at the start, so the body might never run
Q5
A — FOR Index ← 1 TO 30
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 10 → A count-controlled loop: the number of repeats is known (10) before it starts
Q2
REPEAT ... UNTIL Guess = Secret → A condition-controlled loop that tests at the end, so the body always runs at least once
Q3
The condition is tested at the start, so the loop body may never run → WHILE Password <> "open" DO
Q4
The number of repeats is known: read exactly 5 marks → FOR Index ← 1 TO 5
Q5
WHILE Total < 100 DO INPUT Num Total ← Total + Num NEXT → A WHILE loop must end with ENDWHILE, not NEXT. NEXT belongs to a FOR loop.
Q6
FOR Count ← 1 TO Answer <> 5 → FOR needs a known number as its end value, not a condition. A condition-based repeat needs WHILE or REPEAT.
Section C: Definitions
Q1
FOR, WHILE, REPEAT — Use FOR when the number of repeats is known. Use WHILE (tests at the start) or REPEAT (tests at the end) when repeating depends on a condition.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
WHILE Number < 0 DO INPUT Number ENDWHILE
Q3
REPEAT INPUT Guess UNTIL Guess = 7
Q4
WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Q5
FOR Day ← 1 TO 7 OUTPUT Day NEXT Day
Section E: Spot the Error
Q1
D — A FOR loop ends with NEXT and the loop variable, not ENDFOR. The line should be NEXT Student.
Q2
D — A WHILE loop (condition-controlled) must end with ENDWHILE. NEXT is only used to close a FOR loop.
Q3
B — The loop ends with UNTIL, so it must start with REPEAT, not WHILE. The line should be REPEAT.
Q4
C — WHILE repeats while its condition is true. With = -1 the loop would not run for normal marks. It should be WHILE Mark <> -1 DO.
Q5
B — A FOR loop sets its start value with the assignment arrow ←, not =. It should be FOR Num ← 1 TO 10.