Error: loop that never ends

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

Error: loop that never ends

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE Why does this loop never end?
A OUTPUT cannot be used inside a WHILE loop
B Nothing in the body changes Count, so Count <= 5 is always TRUE
2. Which change stops this loop from running forever? Num ← 10 WHILE Num > 0 DO OUTPUT Num ENDWHILE
A Add Num ← Num - 1 inside the loop
B Add OUTPUT Num after ENDWHILE
3. Total ← 0 REPEAT INPUT Number Total ← Total + Number UNTIL Total < 0 The user only enters positive numbers. What is the problem?
A The UNTIL condition can never become TRUE, so the loop never ends
B REPEAT loops must always run exactly once
4. Which loop has the test in the wrong direction and so never ends?
A Count ← 1 WHILE Count <= 10 DO Count ← Count + 1 ENDWHILE
B Count ← 1 WHILE Count >= 1 DO Count ← Count + 1 ENDWHILE
5. A WHILE loop asks for a password: INPUT Password WHILE Password <> "abc123" DO OUTPUT "Try again" ENDWHILE What must be added to the body so it can end?
A INPUT Password
B OUTPUT Password

Match the Pairs

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

Left columnRight column
1WHILE Count <= 5 DO OUTPUT Count ENDWHILE AWrong direction: Count goes up away from 0, so Count > 0 is always TRUE. Fix by using Count ← Count - 1.
2REPEAT INPUT Num UNTIL Num = 0 AND Num = 1 BInfinite loop: Count is never changed in the body, so Count <= 5 stays TRUE forever
3Adding an update to the loop body makes the tested variable move towards the exit value, so the loop ends CThe UNTIL condition can never be TRUE because no number is both above 100 and below 0. Fix by using OR instead of AND.
4Putting INPUT inside the loop body lets the user change the tested variable so the exit value can be reached DInfinite loop: the UNTIL condition can never be TRUE because Num cannot be 0 and 1 at once
5Count ← 10 WHILE Count > 0 DO Count ← Count + 1 ENDWHILE EWHILE Answer <> "Q" DO OUTPUT "Enter Q to quit" INPUT Answer ENDWHILE
6REPEAT INPUT Score UNTIL Score > 100 AND Score < 0 FWHILE Count <= 5 DO OUTPUT Count Count ← Count + 1 ENDWHILE

Definitions

infinite loop

Fill in the Blanks

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

1.
WHILE Count < 5 DO Count ← _____ + 1 ENDWHILE
2.
WHILE Num _ 10 DO Num ← Num + 1 ENDWHILE
3.
REPEAT Num ← Num - 1 UNTIL Num __ 0
4.
WHILE Answer <> "yes" DO _____ Answer ENDWHILE
5.
REPEAT INPUT Guess _____ Guess = 7

Spot the Error

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

Question 1
A Count ← 1
B WHILE Count <= 5 DO
C OUTPUT Count
D Count ← Count - 1
E ENDWHILE
Question 2
A Num ← 2
B WHILE Num > 0 DO
C OUTPUT Num
D Num ← Num + 2
E ENDWHILE
Question 3
A WHILE TRUE DO
B OUTPUT "Driving"
C Fuel ← Fuel - 5
D ENDWHILE
Question 4
A Count ← 0
B REPEAT
C Count ← 1
D OUTPUT Count
E UNTIL Count = 10
Question 5
A Score ← 0
B REPEAT
C Score ← Score + 5
D UNTIL Score = 102

Answer Key

Error: loop that never ends
Section A: Quick Questions
Q1
B — Nothing in the body changes Count, so Count <= 5 is always TRUE
Q2
A — Add Num ← Num - 1 inside the loop
Q3
A — The UNTIL condition can never become TRUE, so the loop never ends
Q4
B — Count ← 1 WHILE Count >= 1 DO Count ← Count + 1 ENDWHILE
Q5
A — INPUT Password
Section B: Match the Pairs
Q1
WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Infinite loop: Count is never changed in the body, so Count <= 5 stays TRUE forever
Q2
REPEAT INPUT Num UNTIL Num = 0 AND Num = 1 → Infinite loop: the UNTIL condition can never be TRUE because Num cannot be 0 and 1 at once
Q3
Adding an update to the loop body makes the tested variable move towards the exit value, so the loop ends → WHILE Count <= 5 DO OUTPUT Count Count ← Count + 1 ENDWHILE
Q4
Putting INPUT inside the loop body lets the user change the tested variable so the exit value can be reached → WHILE Answer <> "Q" DO OUTPUT "Enter Q to quit" INPUT Answer ENDWHILE
Q5
Count ← 10 WHILE Count > 0 DO Count ← Count + 1 ENDWHILE → Wrong direction: Count goes up away from 0, so Count > 0 is always TRUE. Fix by using Count ← Count - 1.
Q6
REPEAT INPUT Score UNTIL Score > 100 AND Score < 0 → The UNTIL condition can never be TRUE because no number is both above 100 and below 0. Fix by using OR instead of AND.
Section C: Definitions
Q1
infinite loop — A loop that never ends because nothing in the body changes the tested variable, or the condition can never reach its exit value.
Section D: Fill in the Blanks
Q1
WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Q2
WHILE Num < 10 DO Num ← Num + 1 ENDWHILE
Q3
REPEAT Num ← Num - 1 UNTIL Num <= 0
Q4
WHILE Answer <> "yes" DO INPUT Answer ENDWHILE
Q5
REPEAT INPUT Guess UNTIL Guess = 7
Section E: Spot the Error
Q1
D — Count goes down, so it moves away from the exit value and Count <= 5 stays TRUE forever. The update must be Count ← Count + 1.
Q2
B — Num only increases, so Num > 0 is always TRUE and the loop never stops. The test points the wrong way. It should be WHILE Num < 20 DO.
Q3
A — The condition TRUE can never become FALSE, so the loop never ends whatever the body does. It should test the variable that changes: WHILE Fuel > 0 DO.
Q4
C — Count is reset to 1 on every pass, so it never reaches 10 and UNTIL Count = 10 is never TRUE. The body must add to Count: Count ← Count + 1.
Q5
D — Score goes up in steps of 5 (..., 100, 105), so it never equals 102. The UNTIL condition never becomes TRUE. It should be UNTIL Score = 100.