A WHILE loop with a compound condition

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

A WHILE loop with a compound condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. WHILE Attempts < 3 AND Correct = FALSE DO. Attempts is 1 and Correct is FALSE. What happens?
A The loop stops, because Correct is FALSE
B The loop repeats, because both tests are TRUE
2. WHILE Attempts < 3 AND Correct = FALSE DO. Attempts is 2 and Correct has just become TRUE. What happens?
A The loop repeats, because Attempts < 3 is still TRUE
B The loop stops, because Correct = FALSE is now FALSE
3. WHILE Attempts < 3 AND Correct = FALSE DO. Attempts is 3 and Correct is still FALSE. What happens?
A The loop stops, because Attempts < 3 is FALSE
B The loop repeats, because Correct = FALSE is TRUE
4. A WHILE loop uses AND to join two tests. When does the loop keep repeating?
A Only while both tests are TRUE
B While at least one test is TRUE
5. WHILE NOT Found AND Index <= 10 DO. Found is FALSE and Index is 4. What happens?
A The loop repeats, because NOT Found is TRUE and Index <= 10 is TRUE
B The loop stops, because Found is FALSE

Match the Pairs

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

Left columnRight column
1WHILE Attempts < 3 AND Correct = FALSE DO AWHILE NOT Found AND Index <= 10 DO
2WHILE NOT Found AND Index <= 10 DO BKeep looping only while the student has had fewer than 3 attempts and has not yet got the answer right.
3The loop stops as soon as either part of an AND condition becomes FALSE, so a password game ends after 3 tries or a correct guess. CCorrect = TRUE is the wrong test. The loop would stop at once because Correct starts as FALSE. It should be Correct = FALSE so the loop continues until the answer is right.
4A search loop should continue only while the item is still not found and there are still places left to check. DFound starts as FALSE, so the condition is FALSE straight away and the loop never runs. It should be NOT Found so the search continues until the item is found.
5WHILE Attempts < 3 AND Correct = TRUE DO EKeep searching while the item has not been found and the index has not gone past 10.
6WHILE Found AND Index <= 10 DO FWHILE Attempts < 3 AND Correct = FALSE DO

Definitions

WHILE Attempts < 3 AND Correct = FALSE DO

Fill in the Blanks

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

1.
WHILE Attempts < 3 ___ Correct = FALSE DO
2.
WHILE ___ Found AND Index <= 10 DO
3.
WHILE Attempts _ 3 AND Correct = FALSE DO
4.
WHILE NOT Found AND Index <= 10 DO Index ← Index + 1 ________
5.
WHILE Attempts < 3 AND Correct = FALSE DO ________ ← Attempts + 1 ENDWHILE

Spot the Error

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

Question 1
A Found ← FALSE
B I ← 1
C WHILE NOT Found OR I <= 10 DO
D Found ← Check(I)
E I ← I + 1
F ENDWHILE
Question 2
A Lives ← 0
B Pts ← 0
C WHILE Lives > 0 AND Pts < 9 DO
D Lives ← Lives - 1
E Pts ← Pts + 2
F ENDWHILE
Question 3
A Tries ← 0
B Ok ← FALSE
C WHILE Tries < 3 AND Ok DO
D Ok ← CheckPin()
E Tries ← Tries + 1
F ENDWHILE
Question 4
A Sum ← 100
B N ← 0
C WHILE Sum < 100 AND N < 5 DO
D Sum ← Sum + Next()
E N ← N + 1
F ENDWHILE
Question 5
A Fuel ← 10
B Km ← 0
C WHILE Fuel > 0 AND Km < 50
D Fuel ← Fuel - 1
E Km ← Km + 8
F ENDWHILE

Answer Key

A WHILE loop with a compound condition
Section A: Quick Questions
Q1
B — The loop repeats, because both tests are TRUE
Q2
B — The loop stops, because Correct = FALSE is now FALSE
Q3
A — The loop stops, because Attempts < 3 is FALSE
Q4
A — Only while both tests are TRUE
Q5
A — The loop repeats, because NOT Found is TRUE and Index <= 10 is TRUE
Section B: Match the Pairs
Q1
WHILE Attempts < 3 AND Correct = FALSE DO → Keep looping only while the student has had fewer than 3 attempts and has not yet got the answer right.
Q2
WHILE NOT Found AND Index <= 10 DO → Keep searching while the item has not been found and the index has not gone past 10.
Q3
The loop stops as soon as either part of an AND condition becomes FALSE, so a password game ends after 3 tries or a correct guess. → WHILE Attempts < 3 AND Correct = FALSE DO
Q4
A search loop should continue only while the item is still not found and there are still places left to check. → WHILE NOT Found AND Index <= 10 DO
Q5
WHILE Attempts < 3 AND Correct = TRUE DO → Correct = TRUE is the wrong test. The loop would stop at once because Correct starts as FALSE. It should be Correct = FALSE so the loop continues until the answer is right.
Q6
WHILE Found AND Index <= 10 DO → Found starts as FALSE, so the condition is FALSE straight away and the loop never runs. It should be NOT Found so the search continues until the item is found.
Section C: Definitions
Q1
WHILE Attempts < 3 AND Correct = FALSE DO — The loop repeats only while BOTH tests are TRUE. As soon as either test becomes FALSE, the whole condition is FALSE and the loop stops.
Section D: Fill in the Blanks
Q1
WHILE Attempts < 3 AND Correct = FALSE DO
Q2
WHILE NOT Found AND Index <= 10 DO
Q3
WHILE Attempts < 3 AND Correct = FALSE DO
Q4
WHILE NOT Found AND Index <= 10 DO Index ← Index + 1 ENDWHILE
Q5
WHILE Attempts < 3 AND Correct = FALSE DO Attempts ← Attempts + 1 ENDWHILE
Section E: Spot the Error
Q1
C — The loop should continue only while BOTH tests are TRUE. With OR, it keeps going if either test is TRUE. If the item is never found, NOT Found stays TRUE, so the loop never stops. It should be AND.
Q2
A — Lives must start above 0. If Lives starts at 0, Lives > 0 is FALSE straight away, so the whole AND condition is FALSE and the loop never runs. It should be Lives ← 3.
Q3
C — Ok starts as FALSE, so the second test is FALSE and the AND condition is FALSE at once. The loop should continue while the answer is NOT yet correct, so it needs NOT Ok.
Q4
A — Sum must start at 0. If it starts at 100, Sum < 100 is FALSE straight away, so the AND condition is FALSE and the loop body never runs. It should be Sum ← 0.
Q5
C — The WHILE line is missing the keyword DO at the end. It should be WHILE Fuel > 0 AND Km < 50 DO.