A WHILE loop ended by a rogue value

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

A WHILE loop ended by a rogue value

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A WHILE loop reads numbers until the user types -1. What is -1 called?
A A rogue (sentinel) value
B A counter
2. Which layout reads input correctly for a rogue-value loop?
A INPUT Number, then WHILE Number <> -1 DO, then INPUT Number again at the end of the body
B WHILE Number <> -1 DO, then INPUT Number only at the start of the body
3. Why is there an INPUT before the WHILE loop starts?
A So the loop can count how many times it runs
B So the condition has a first value to test
4. Which condition keeps the loop running until the user types STOP?
A WHILE Name = "STOP" DO
B WHILE Name <> "STOP" DO
5. A user types 5, 8, 2 and then -1. The loop adds each number to Total. What is Total?
A 15
B 14

Match the Pairs

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

Left columnRight column
1WHILE Number <> -1 DO AINPUT Word WHILE Word <> "STOP" DO Count <- Count + 1 INPUT Word ENDWHILE
2INPUT Number WHILE Number <> -1 DO BINPUT is at the start of the body, so the rogue value -1 is added to the total before the loop condition is checked. It should be read at the end of the body.
3Read the next value at the end of the loop body so the rogue value can be checked on the next test CKeeps looping until the user types the rogue value -1, which signals the end of the input and is not real data
4A text rogue value such as STOP ends the loop and is never added to the total or counted DThere is no INPUT before the loop or inside it, so Number never changes and the loop never ends (or Number has no value to test).
5WHILE Number <> -1 DO INPUT Number Total <- Total + Number ENDWHILE EThe first value is read before the loop so the WHILE condition has something to test, even if it is the rogue value straight away
6WHILE Number <> -1 DO Total <- Total + Number ENDWHILE FWHILE Number <> -1 DO Total <- Total + Number INPUT Number ENDWHILE

Definitions

WHILE Number <> -1 DO

Fill in the Blanks

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

1.
INPUT Number WHILE Number <> __ DO Total <- Total + Number
2.
INPUT Number _____ Number <> -1 DO Total <- Total + Number
3.
Total <- Total + Number _____ Number ENDWHILE
4.
INPUT Number WHILE Number __ -1 DO Count <- Count + 1
5.
WHILE Number <> -1 DO Count <- _____ + 1 INPUT Number

Spot the Error

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

Question 1
A Total <- 1
B INPUT Number
C WHILE Number <> -1 DO
D Total <- Total + Number
E INPUT Number
F ENDWHILE
Question 2
A DECLARE Mark : INTEGER
B INPUT Mrk
C WHILE Mark <> -1 DO
D OUTPUT Mark
E INPUT Mark
F ENDWHILE
Question 3
A INPUT Number
B Total <- 0
C WHILE Number = -1 DO
D Total <- Total + Number
E INPUT Number
F ENDWHILE
Question 4
A Count <- 0
B INPUT Mark
C WHILE Mark <> -1
D Count <- Count + 1
E INPUT Mark
F ENDWHILE
Question 5
A Sum <- 0
B INPUT Score
C WHILE Score <> -1 DO
D Sum <- Score
E INPUT Score
F ENDWHILE

Answer Key

A WHILE loop ended by a rogue value
Section A: Quick Questions
Q1
A — A rogue (sentinel) value
Q2
A — INPUT Number, then WHILE Number <> -1 DO, then INPUT Number again at the end of the body
Q3
B — So the condition has a first value to test
Q4
B — WHILE Name <> "STOP" DO
Q5
A — 15
Section B: Match the Pairs
Q1
WHILE Number <> -1 DO → Keeps looping until the user types the rogue value -1, which signals the end of the input and is not real data
Q2
INPUT Number WHILE Number <> -1 DO → The first value is read before the loop so the WHILE condition has something to test, even if it is the rogue value straight away
Q3
Read the next value at the end of the loop body so the rogue value can be checked on the next test → WHILE Number <> -1 DO Total <- Total + Number INPUT Number ENDWHILE
Q4
A text rogue value such as STOP ends the loop and is never added to the total or counted → INPUT Word WHILE Word <> "STOP" DO Count <- Count + 1 INPUT Word ENDWHILE
Q5
WHILE Number <> -1 DO INPUT Number Total <- Total + Number ENDWHILE → INPUT is at the start of the body, so the rogue value -1 is added to the total before the loop condition is checked. It should be read at the end of the body.
Q6
WHILE Number <> -1 DO Total <- Total + Number ENDWHILE → There is no INPUT before the loop or inside it, so Number never changes and the loop never ends (or Number has no value to test).
Section C: Definitions
Q1
WHILE Number <> -1 DO — Loop that repeats until the user types a rogue (sentinel) value such as -1. INPUT before the loop and again at the end of the body; the rogue value is not counted or totalled.
Section D: Fill in the Blanks
Q1
INPUT Number WHILE Number <> -1 DO Total <- Total + Number
Q2
INPUT Number WHILE Number <> -1 DO Total <- Total + Number
Q3
Total <- Total + Number INPUT Number ENDWHILE
Q4
INPUT Number WHILE Number <> -1 DO Count <- Count + 1
Q5
WHILE Number <> -1 DO Count <- Count + 1 INPUT Number
Section E: Spot the Error
Q1
A — The total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be Total <- 0.
Q2
B — The first INPUT before the loop reads into the wrong variable. Mrk is not Mark, so the loop test uses a value that was never read. It should be INPUT Mark.
Q3
C — The loop must keep going while Number is NOT the rogue value. With = it would only run when -1 is typed, so real data is skipped. It should be WHILE Number <> -1 DO.
Q4
C — A WHILE loop header must end with DO. It should be WHILE Mark <> -1 DO.
Q5
D — This replaces the sum each time instead of adding to it, so only the last score is kept. It should be Sum <- Sum + Score.