Choosing count-controlled, pre-condition or post-condition loops

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

Choosing count-controlled, pre-condition or post-condition loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program must display the 12 times table (12 lines of output). Which loop fits best?
A FOR Count <- 1 TO 12
B WHILE Count <> 12 with no counter set
2. A program reads marks until the user types -1 (a rogue value). Which loop is best?
A WHILE Mark <> -1 DO
B FOR Mark <- 1 TO -1
3. A password must be asked for at least once, and again if it is wrong. Which loop is best?
A FOR Attempt <- 1 TO 0
B REPEAT ... UNTIL Password = Correct
4. Which statement about a WHILE loop is true?
A It always runs at least once
B It may run zero times if the condition is false at the start
5. Which statement about REPEAT...UNTIL is true?
A The condition is checked after the loop body has run
B The condition is checked before the loop body runs

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 30 INPUT Mark NEXT Count AA FOR loop is the wrong choice because the number of attempts is not known in advance. The user should be asked until the password is correct, so use REPEAT...UNTIL Password = "open".
2REPEAT INPUT Password UNTIL Password = "open" BA post-condition loop: use it when the body must run at least once, such as asking for input before checking it.
3Read numbers until the rogue value -1 is entered; the loop may run zero times if -1 is typed first, so use a pre-condition loop. CREPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120
4Validate an input so that the user is always asked at least once and asked again until the value is acceptable. DA count-controlled loop: use it when the number of repeats is known in advance, such as 30 students.
5WHILE Count <= 10 DO OUTPUT Count ENDWHILE ECount is never given a start value and never increased, so the loop never ends (or never starts). The number of repeats is known, so a FOR loop is the better choice: FOR Count <- 1 TO 10.
6FOR Attempt <- 1 TO 100 INPUT Password NEXT Attempt FINPUT Num WHILE Num <> -1 DO Total <- Total + Num INPUT Num ENDWHILE

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.
_____ Num <> -1 DO Total ← Total + Num ENDWHILE
3.
REPEAT INPUT Age _____ Age >= 1 AND Age <= 120
4.
______ INPUT Password UNTIL Password = "open"
5.
FOR Student ← 1 TO 30 INPUT Mark ____ Student

Spot the Error

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

Question 1
A FOR Count <- 1 UNTIL 10
B INPUT Mark
C Total <- Total + Mark
D NEXT Count
Question 2
A INPUT Score
B WHILE Score = -1 DO
C Total <- Total + Score
D INPUT Score
E ENDWHILE
Question 3
A OUTPUT "Enter age 0-120"
B WHILE
C INPUT Age
D UNTIL Age >= 0 AND Age <= 120
Question 4
A Count <- 0
B WHILE Count < 5 DO
C Count <- Count + 1
D NEXT Count
Question 5
A REPEAT
B INPUT Password
C Tries <- Tries + 1
D ENDWHILE

Answer Key

Choosing count-controlled, pre-condition or post-condition loops
Section A: Quick Questions
Q1
A — FOR Count <- 1 TO 12
Q2
A — WHILE Mark <> -1 DO
Q3
B — REPEAT ... UNTIL Password = Correct
Q4
B — It may run zero times if the condition is false at the start
Q5
A — The condition is checked after the loop body has run
Section B: Match the Pairs
Q1
FOR Count <- 1 TO 30 INPUT Mark NEXT Count → A count-controlled loop: use it when the number of repeats is known in advance, such as 30 students.
Q2
REPEAT INPUT Password UNTIL Password = "open" → A post-condition loop: use it when the body must run at least once, such as asking for input before checking it.
Q3
Read numbers until the rogue value -1 is entered; the loop may run zero times if -1 is typed first, so use a pre-condition loop. → INPUT Num WHILE Num <> -1 DO Total <- Total + Num INPUT Num ENDWHILE
Q4
Validate an input so that the user is always asked at least once and asked again until the value is acceptable. → REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120
Q5
WHILE Count <= 10 DO OUTPUT Count ENDWHILE → Count is never given a start value and never increased, so the loop never ends (or never starts). The number of repeats is known, so a FOR loop is the better choice: FOR Count <- 1 TO 10.
Q6
FOR Attempt <- 1 TO 100 INPUT Password NEXT Attempt → A FOR loop is the wrong choice because the number of attempts is not known in advance. The user should be asked until the password is correct, so use REPEAT...UNTIL Password = "open".
Section C: Definitions
Q1
FOR, WHILE or REPEAT — Choose FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT...UNTIL when the loop must run at least once.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Q2
WHILE Num <> -1 DO Total ← Total + Num ENDWHILE
Q3
REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 120
Q4
REPEAT INPUT Password UNTIL Password = "open"
Q5
FOR Student ← 1 TO 30 INPUT Mark NEXT Student
Section E: Spot the Error
Q1
A — The number of repeats is known (10), so FOR is the right loop. A FOR loop uses TO to give the end value, not UNTIL. It should be FOR Count <- 1 TO 10.
Q2
B — A WHILE loop that reads until a rogue value must keep going while the value is NOT the rogue value. It should be WHILE Score <> -1 DO. As written, the loop never runs for normal scores.
Q3
B — Input validation must ask at least once, so a REPEAT...UNTIL loop is used. The loop must start with REPEAT to match the UNTIL at the end. WHILE needs DO and ENDWHILE.
Q4
D — NEXT closes a FOR loop. A WHILE loop must be closed with ENDWHILE.
Q5
D — A post-condition loop ends with UNTIL and its condition, not ENDWHILE. The correct line is UNTIL Password = Secret. It repeats until the password matches.