Error: wrong loop type

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

Error: wrong loop type

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A loop must be able to run zero times if there is no data. Which loop type is correct?
A WHILE ... DO ... ENDWHILE
B REPEAT ... UNTIL
2. A program reads scores into Total using REPEAT ... UNTIL Score = -1. The user types -1 straight away because there is no data. What goes wrong?
A The -1 is added to Total, because REPEAT runs its body once before testing
B The loop is skipped and the body never runs
3. A menu must be shown at least once before the user makes a choice. Which loop type is correct?
A WHILE ... DO ... ENDWHILE
B REPEAT ... UNTIL
4. WHILE Answer <> "Y" DO is written, but Answer has not been given a value by INPUT yet. What is the error?
A WHILE tests Answer before the first INPUT, so the condition is checked on a missing value
B WHILE always runs once, so the test is not needed
5. Which is a correct fix for a WHILE loop that tests Number before any INPUT?
A Put INPUT Number after ENDWHILE
B Put INPUT Number before the loop and again at the end of the loop body

Match the Pairs

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

Left columnRight column
1REPEAT INPUT Mark Total ← Total + Mark UNTIL Mark = -1 AREPEAT INPUT Password UNTIL Password = "abc"
2WHILE Answer <> "yes" DO OUTPUT "Go again?" INPUT Answer ENDWHILE BWrong loop type: REPEAT runs with no data. If the first Score is 0 the sentinel is still counted. Use INPUT first, then WHILE Score <> 0 DO.
3Correct loop type when the body must run at least once: read the password first, then test it CWrong loop type: WHILE is tested before the first INPUT, so Guess has no value yet. Change to REPEAT INPUT Guess UNTIL Guess = Secret.
4Correct loop type when the body might not run at all: test the first INPUT before doing any work DINPUT Mark WHILE Mark <> -1 DO Total ← Total + Mark INPUT Mark ENDWHILE
5REPEAT INPUT Score Count ← Count + 1 UNTIL Score = 0 EWHILE tests first, so if Answer has no value yet the check is wrong and the loop may never run. REPEAT ... UNTIL is needed.
6WHILE Guess <> Secret DO INPUT Guess ENDWHILE FREPEAT always runs once, so if -1 is the first value typed, it is still added to Total. A WHILE loop is needed here.

Definitions

wrong loop type

Fill in the Blanks

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

1.
Count ← 0 _____ Count < NumItems DO Count ← Count + 1
2.
INPUT Age WHILE Age < 0 OR Age > 120 __ OUTPUT "Invalid age"
3.
______ INPUT Password UNTIL Password = "Secret"
4.
REPEAT INPUT Mark UNTIL Mark >= 0 ___ Mark <= 100
5.
INPUT Number WHILE Number __ 0 DO Total ← Total + Number

Spot the Error

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

Question 1
A REPEAT
B Stock ← Stock - 1
C OUTPUT "Sold one"
D ENDWHILE
Question 2
A DECLARE Pin : INTEGER
B OUTPUT "Enter your PIN"
C WHILE Pin <> 1234 DO
D INPUT Pin
E UNTIL Pin = 1234
Question 3
A Total ← 0
B INPUT Mark
C REPEAT
D Total ← Total + Mark
E INPUT Mark
F ENDWHILE
Question 4
A INPUT Target
B Distance ← 0
C OUTPUT "Start walking"
D REPEAT
E Distance ← Distance + 2
F ENDWHILE
Question 5
A INPUT Seats
B REPEAT
C OUTPUT "Seat sold"
D Seats ← Seats - 1
E ENDWHILE

Answer Key

Error: wrong loop type
Section A: Quick Questions
Q1
A — WHILE ... DO ... ENDWHILE
Q2
A — The -1 is added to Total, because REPEAT runs its body once before testing
Q3
B — REPEAT ... UNTIL
Q4
A — WHILE tests Answer before the first INPUT, so the condition is checked on a missing value
Q5
B — Put INPUT Number before the loop and again at the end of the loop body
Section B: Match the Pairs
Q1
REPEAT INPUT Mark Total ← Total + Mark UNTIL Mark = -1 → REPEAT always runs once, so if -1 is the first value typed, it is still added to Total. A WHILE loop is needed here.
Q2
WHILE Answer <> "yes" DO OUTPUT "Go again?" INPUT Answer ENDWHILE → WHILE tests first, so if Answer has no value yet the check is wrong and the loop may never run. REPEAT ... UNTIL is needed.
Q3
Correct loop type when the body must run at least once: read the password first, then test it → REPEAT INPUT Password UNTIL Password = "abc"
Q4
Correct loop type when the body might not run at all: test the first INPUT before doing any work → INPUT Mark WHILE Mark <> -1 DO Total ← Total + Mark INPUT Mark ENDWHILE
Q5
REPEAT INPUT Score Count ← Count + 1 UNTIL Score = 0 → Wrong loop type: REPEAT runs with no data. If the first Score is 0 the sentinel is still counted. Use INPUT first, then WHILE Score <> 0 DO.
Q6
WHILE Guess <> Secret DO INPUT Guess ENDWHILE → Wrong loop type: WHILE is tested before the first INPUT, so Guess has no value yet. Change to REPEAT INPUT Guess UNTIL Guess = Secret.
Section C: Definitions
Q1
wrong loop type — Using REPEAT when the body must sometimes not run, or WHILE when it must run at least once. REPEAT always runs once; WHILE may run zero times.
Section D: Fill in the Blanks
Q1
Count ← 0 WHILE Count < NumItems DO Count ← Count + 1
Q2
INPUT Age WHILE Age < 0 OR Age > 120 DO OUTPUT "Invalid age"
Q3
REPEAT INPUT Password UNTIL Password = "Secret"
Q4
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q5
INPUT Number WHILE Number <> 0 DO Total ← Total + Number
Section E: Spot the Error
Q1
A — Stock might already be 0, so the body must be able to run zero times. REPEAT always runs once and would sell an item that does not exist. Use WHILE Stock > 0 DO (and end with ENDWHILE).
Q2
C — Pin has no value before the first INPUT, and WHILE tests before the body runs, so the INPUT may never happen. The body must run at least once, so use REPEAT (ending with UNTIL Pin = 1234).
Q3
C — The user may type -1 straight away, meaning there is no data. REPEAT would still run once and add -1 to Total. WHILE Mark <> -1 DO tests first, so the body is skipped when there is no data.
Q4
D — If Target is 0 the walker should not move at all. REPEAT always runs the body once, so Distance would increase when it should not. Use WHILE Distance < Target DO so the body can run zero times.
Q5
B — If there are 0 seats left, nothing should be sold. REPEAT runs the body once before testing, so it would sell a seat that does not exist. Use WHILE Seats > 0 DO.