Choosing the right loop

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

Choosing the right loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program must output the 12 times table, one line for each number from 1 to 12. Which loop is the best choice?
A FOR Count ← 1 TO 12
B REPEAT ... UNTIL
2. Which statement is correct?
A Use FOR when the number of repeats is known in advance
B Use FOR when the user decides when to stop by typing -1
3. A program reads marks until the user types -1 (the sentinel). The first value typed might already be -1. Which loop is best?
A WHILE Mark <> -1 DO
B REPEAT ... UNTIL Mark = -1
4. A program asks for a password and must ask at least once, repeating if it is wrong. Which loop is best?
A WHILE Password <> Correct DO (with nothing asked first)
B REPEAT ... UNTIL Password = Correct
5. Which loop can have a body that runs zero times?
A REPEAT
B WHILE

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 OUTPUT Count NEXT Count AINPUT Num WHILE Num <> -1 DO Total ← Total + Num INPUT Num ENDWHILE
2REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 BA REPEAT loop: the body must run at least once, so it suits a validation prompt.
3Read values until a sentinel of -1 is entered, where the loop might not run at all if -1 is entered first CA FOR loop: the number of repeats is known in advance (exactly 10 times).
4Ask for a password again and again until the entry is accepted, always asking at least once DA FOR loop is used when the number of repeats is not known. A WHILE loop with a sentinel value should be used instead.
5FOR Count ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Count // used when the number of inputs is unknown EWHILE tests before the body, so the prompt may never run because Age has no value yet. REPEAT ... UNTIL should be used so the input always happens at least once.
6WHILE Age < 0 OR Age > 120 DO INPUT Age ENDWHILE // validation, Age not yet set FREPEAT INPUT Password UNTIL Password = "Secret"

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.
INPUT Mark _____ Mark <> -1 DO INPUT Mark
3.
REPEAT INPUT Age _____ Age >= 0 AND Age <= 120
4.
FOR Day ← 1 TO 7 INPUT Temp ____ Day
5.
REPEAT INPUT Pin UNTIL Pin _ 1234

Spot the Error

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

Question 1
A Total ← 0
B OUTPUT "Enter 5 marks"
C FOR Count ← 1 TO 4
D INPUT Mark
E Total ← Total + Mark
F NEXT Count
Question 2
A OUTPUT Num
B WHILE Num <> -1 DO
C Total ← Total + Num
D INPUT Num
E ENDWHILE
Question 3
A REPEAT
B OUTPUT "Enter age 1-100"
C INPUT Age
D UNTIL Age >= 1 OR Age <= 100
Question 4
A Count ← 0
B INPUT Name
C WHILE Name = "STOP" DO
D Count ← Count + 1
E INPUT Name
F ENDWHILE
Question 5
A OUTPUT "Enter 1 to 10"
B WHILE
C INPUT Num
D UNTIL Num >= 1 AND Num <= 10

Answer Key

Choosing the right loop
Section A: Quick Questions
Q1
A — FOR Count ← 1 TO 12
Q2
A — Use FOR when the number of repeats is known in advance
Q3
A — WHILE Mark <> -1 DO
Q4
B — REPEAT ... UNTIL Password = Correct
Q5
B — WHILE
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 10 OUTPUT Count NEXT Count → A FOR loop: the number of repeats is known in advance (exactly 10 times).
Q2
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 → A REPEAT loop: the body must run at least once, so it suits a validation prompt.
Q3
Read values until a sentinel of -1 is entered, where the loop might not run at all if -1 is entered first → INPUT Num WHILE Num <> -1 DO Total ← Total + Num INPUT Num ENDWHILE
Q4
Ask for a password again and again until the entry is accepted, always asking at least once → REPEAT INPUT Password UNTIL Password = "Secret"
Q5
FOR Count ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Count // used when the number of inputs is unknown → A FOR loop is used when the number of repeats is not known. A WHILE loop with a sentinel value should be used instead.
Q6
WHILE Age < 0 OR Age > 120 DO INPUT Age ENDWHILE // validation, Age not yet set → WHILE tests before the body, so the prompt may never run because Age has no value yet. REPEAT ... UNTIL should be used so the input always happens at least once.
Section C: Definitions
Q1
FOR / WHILE / REPEAT — Use FOR when the number of repeats is known, WHILE when the body might run zero times (such as reading until a sentinel), and REPEAT when the body must run at least once (such as validation).
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
INPUT Mark WHILE Mark <> -1 DO INPUT Mark
Q3
REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120
Q4
FOR Day ← 1 TO 7 INPUT Temp NEXT Day
Q5
REPEAT INPUT Pin UNTIL Pin = 1234
Section E: Spot the Error
Q1
C — The count is known to be 5, so FOR is the right loop. But TO 4 makes it run only 4 times. It should be FOR Count ← 1 TO 5.
Q2
A — A WHILE loop tests its condition before the first pass, so the first value must be read with INPUT Num before the loop. OUTPUT does not read a value.
Q3
D — The validation loop must repeat until the age is inside the range, so both conditions must be true. OR is true for almost any number, so it should be AND.
Q4
C — The loop must keep running until the sentinel "STOP" is entered. With = the body is skipped for any other name, so it should be <>.
Q5
B — Validation must ask at least once, so a REPEAT loop is needed. REPEAT is closed by UNTIL, so WHILE does not match it and should be REPEAT.