Condition-Controlled Loops · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Looping until the answer is right

Computing · Programming · ages 15-16
Name ______________________   Date ____________
  1. In the password loop, which line lets it end?

    • The line printing the final message
    • The line setting the first flag value
    • The line asking for a new entry inside the loop
  2. A program must keep asking until the right password is typed. Which loop fits?

    • A while loop on the entry matching
    • A for loop over ten fixed tries
    • No loop at all
  3. If nothing inside the loop can change the condition, it runs forever.

    Circle one:   True   False

  4. Read sensor data until it reports ready. The wait length is unknown. Which fits?

    • A for loop over a fixed range
    • A single if statement
    • A while loop on the ready signal
  5. Print every name in a class list of known size. Which loop fits and why?

    • For, since the list size is known
    • While, since names are strings
    • While, since lists always need conditions
  6. A while loop counts down but never stops. What is the likely fault?

    • The counter starts at zero
    • The body never updates the counter
    • The loop prints too much text
  7. A student waits for a password with a for loop over a huge fixed range, hoping it is enough. Why is while better?

    • While waits exactly as long as needed instead of guessing a cap
    • For loops cannot check passwords
    • While runs faster on each pass
  8. n starts at 4. While n is above 0, the loop prints n then sets n to n minus 1. How many prints?

    • 3
    • 4
    • 5
LightMySky · lightmysky.comW1-mt_LjxO2n3Fy6-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Looping until the answer is right W1-mt_LjxO2n3Fy6-s1

  1. The line asking for a new entry inside the loop · Only a fresh entry can flip the condition to False.
  2. A while loop on the entry matching · The tries needed are unknown, so a condition-controlled loop fits.
  3. True · The condition is rechecked every pass, so a frozen condition means endless passes.
  4. A while loop on the ready signal · Unknown waits call for a condition-controlled loop.
  5. For, since the list size is known · Known counts call for a counted loop, which is for.
  6. The body never updates the counter · Without an update inside, the condition can never turn False.
  7. While waits exactly as long as needed instead of guessing a cap · While matches the task shape: stop when the event happens, whenever that is.
  8. 4 · It prints 4, 3, 2 and 1, then stops when n reaches 0.
Worksheet · LightMySky