How many times a FOR loop runs

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

How many times a FOR loop runs

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How many times does FOR i ← 1 TO 5 run its loop body?
A 5 times
B 4 times
2. Which formula gives the number of times a FOR loop runs?
A end - start
B end - start + 1
3. FOR i ← 3 TO 3 ... NEXT i. What happens?
A The loop body never runs
B The loop body runs exactly once
4. FOR i ← 5 TO 1 ... NEXT i. What happens?
A The loop body runs zero times
B The loop body runs 5 times, counting down
5. A student wants a loop to run 10 times. Which loop header is correct?
A FOR i ← 1 TO 10
B FOR i ← 1 TO 9

Match the Pairs

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

Left columnRight column
1FOR i ← 1 TO 5 AFOR Count ← 4 TO 11
2FOR i ← 5 TO 1 BThe loop body runs zero times, because the start value is bigger than the end value
3When the start and end values are equal, the loop runs exactly once COff-by-one error: 1 TO 10 runs 10 times, not 9. The count was worked out as end - start and the + 1 was forgotten.
4The number of repetitions is end - start + 1, so this loop runs 8 times DThe loop body runs 5 times, because 5 - 1 + 1 = 5
5FOR i ← 1 TO 10 // meant to run 9 times EFOR i ← 3 TO 3
6FOR i ← 10 TO 1 // meant to run 10 times FThe start value is bigger than the end value, so the loop runs zero times instead of 10.

Definitions

end - start + 1

Fill in the Blanks

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

1.
FOR i ← 1 TO _ OUTPUT i NEXT i
2.
FOR i ← 3 TO _ OUTPUT i NEXT i
3.
FOR i ← 5 TO 1 // runs _ times OUTPUT i NEXT i
4.
FOR i ← 3 TO 8 // runs 8 - 3 + _ times OUTPUT i NEXT i
5.
FOR i ← 0 TO _ // runs 10 times OUTPUT i NEXT i

Spot the Error

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

Question 1
A Count ← 0
B FOR i ← 1 TO 5
C Count = Count + 1
D NEXT i
E OUTPUT Count
Question 2
A // Runs once
B FOR i ← 3 TO 2
C OUTPUT i
D NEXT i
Question 3
A FOR i ← 1 TO 4
B OUTPUT "Hello"
C NEXT i
D // Hello is shown 5 times
Question 4
A FOR i ← 3 TO 1
B OUTPUT i * 2
C NEXT i
D // Output: 2 4 6
Question 5
A Runs ← 0
B FOR i ← 2 TO 6
C Runs ← Runs + 1
D NEXT Runs
E OUTPUT Runs

Answer Key

How many times a FOR loop runs
Section A: Quick Questions
Q1
A — 5 times
Q2
B — end - start + 1
Q3
B — The loop body runs exactly once
Q4
A — The loop body runs zero times
Q5
A — FOR i ← 1 TO 10
Section B: Match the Pairs
Q1
FOR i ← 1 TO 5 → The loop body runs 5 times, because 5 - 1 + 1 = 5
Q2
FOR i ← 5 TO 1 → The loop body runs zero times, because the start value is bigger than the end value
Q3
When the start and end values are equal, the loop runs exactly once → FOR i ← 3 TO 3
Q4
The number of repetitions is end - start + 1, so this loop runs 8 times → FOR Count ← 4 TO 11
Q5
FOR i ← 1 TO 10 // meant to run 9 times → Off-by-one error: 1 TO 10 runs 10 times, not 9. The count was worked out as end - start and the + 1 was forgotten.
Q6
FOR i ← 10 TO 1 // meant to run 10 times → The start value is bigger than the end value, so the loop runs zero times instead of 10.
Section C: Definitions
Q1
end - start + 1 — The number of times a FOR loop runs. FOR i ← 1 TO 5 runs 5 times, 3 TO 3 runs once, and 5 TO 1 runs zero times because the start value is bigger than the end value.
Section D: Fill in the Blanks
Q1
FOR i ← 1 TO 5 OUTPUT i NEXT i
Q2
FOR i ← 3 TO 3 OUTPUT i NEXT i
Q3
FOR i ← 5 TO 1 // runs 0 times OUTPUT i NEXT i
Q4
FOR i ← 3 TO 8 // runs 8 - 3 + 1 times OUTPUT i NEXT i
Q5
FOR i ← 0 TO 9 // runs 10 times OUTPUT i NEXT i
Section E: Spot the Error
Q1
C — Assignment uses the arrow ←, not =. The line should be Count ← Count + 1, so Count goes up by 1 on each of the 5 runs of the loop.
Q2
B — The loop should run exactly once, which happens when the start and end values are equal. It should be FOR i ← 3 TO 3. With 3 TO 2 the start is bigger than the end, so the loop runs zero times.
Q3
A — This is an off-by-one mistake. 1 TO 4 runs 4 - 1 + 1 = 4 times. To run 5 times the line must be FOR i ← 1 TO 5.
Q4
A — The start value 3 is bigger than the end value 1, so the loop would run zero times and print nothing. It should be FOR i ← 1 TO 3, which runs 3 times.
Q5
D — NEXT must name the loop variable used in the FOR line. It should be NEXT i. The loop runs 6 - 2 + 1 = 5 times, so Runs ends as 5.