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

Loops that know when to stop

Computing · Programming · ages 14-16
Name ______________________   Date ____________
  1. In a loop over range(5), what does the loop variable hold first?

    • 1
    • 5
    • 0
  2. How many times does a loop over range(5) run?

    • 4
    • 5
    • 6
  3. range(5) hands the loop the numbers 0, 1, 2, 3, 4.

    Circle one:   True   False

  4. What does range(10, 26, 5) hand the loop?

    • 10, 15, 20, 25, 26
    • 10, 15, 20, 25
    • 26, 25, 20, 15, 10
  5. A loop prints the 7 times table from 0 to 12 with range(13). How many lines print?

    Answer: ______________

  6. In the 9 times table loop, what does the loop variable hold on each pass?

    • The next count from 0 to 12
    • Always 9
    • Always 0
  7. How many times does a loop over range(3, 12, 3) run?

    Answer: ______________

  8. Sam runs a loop over range(5) expecting 1, 2, 3, 4, 5. What is wrong?

    • It prints nothing
    • The stop is excluded, so it prints 0, 1, 2, 3, 4
    • It prints 1 to 5 twice
LightMySky · lightmysky.comW1-mt_r1eXTWI_6j-s1

Answer key

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

Loops that know when to stop W1-mt_r1eXTWI_6j-s1

  1. 0 · Counting starts at 0, so the first value handed over is 0.
  2. 5 · range(5) holds five numbers, so the body runs five times.
  3. True · A stop of 5 counts up from 0 and stops before 5.
  4. 10, 15, 20, 25 · Start at 10, step by 5, and stop before reaching 26.
  5. 13 · Thirteen values means thirteen passes and thirteen lines.
  6. The next count from 0 to 12 · The variable walks through the range while 9 stays fixed in the body.
  7. 3 · The loop sees 3, 6 and 9, which is three passes.
  8. The stop is excluded, so it prints 0, 1, 2, 3, 4 · The stop value never prints, and counting starts at 0.
Worksheet · LightMySky