Iteration: Count-Controlled
Repeat a block a known number of times with for and range().
A count-controlled loop is suitable when the number of repetitions is known. In Python this is commonly written with for and range().
Predict
Read the code first. Do not run it yet.
total = 0
for i in range(1, 5):
total = total + i
print(total)- Which values will
itake? - How many times does the loop run?
- Predict the final value of
total.
Investigate
Use this code to explain exactly how the program works.
for number in range(2, 11, 2):
print(number)- What are the start, stop and step values?
- Why is 11 not printed?
- How would you change the code to print 3, 6, 9, 12?
Using range()
| Form | Values produced |
|---|---|
range(5) | 0, 1, 2, 3, 4 |
range(2, 6) | 2, 3, 4, 5 |
range(2, 11, 2) | 2, 4, 6, 8, 10 |
for count in range(3):
print("Practice")Practice Practice Practice
range(start, stop) includes the start value but stops before the stop value.Self-study guide
Count-controlled iteration repeats a block a known number of times. Python normally uses a for loop with range(). OCR Exam Reference Language writes an inclusive start and end value, while Python range(start, stop) excludes the stop value.
This end-point difference is one of the most important OCR-to-Python translations. OCR for i=0 to 9 runs ten times; Python needs range(0, 10) to produce the same values.
range() correctly, including a step value when needed.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
for i=0 to 9 ... next i | for i in range(0, 10): | OCR end 9 is inclusive; Python stop 10 is excluded. |
for i=2 to 10 step 2 | for i in range(2, 11, 2): | Use stop 11 so 10 can be produced. |
for i=10 to 0 step -1 | for i in range(10, -1, -1): | Use stop -1 so 0 can be produced. |
Worked example
A running total is a common use of count-controlled iteration.
total = 0
for day in range(1, 6):
minutes = int(input("Minutes: "))
total = total + minutes
print(total)150
range(1, 6)produces 1, 2, 3, 4, 5: five repetitions.totalis created before the loop so it keeps its value between repetitions.- Each new input is added to the previous total.
- The final
print()is not indented because it should run once after the loop.
Another example
A step value changes the loop variable by more than one each time.
for number in range(2, 11, 2):
print(number)2 4 6 8 10
- The start is 2.
- The stop value 11 is excluded.
- The step 2 gives every second integer.
Common mistakes
- Using
range(1, 5)when you intend to include 5. - Indenting code that should run after the loop.
- Changing the loop variable manually when
range()is already controlling it. - Forgetting that OCR FOR endpoints are inclusive but Python range stops are exclusive.
0 to 9 corresponds to Python range(0, 10).Exam-ready checklist
- Choose a count-controlled loop when the repetition count is known.
- Calculate the exact values generated by
range(). - Translate OCR FOR loops to Python and back.
- Use a loop variable, running total and counter correctly.
What you have learned
- Count-controlled iteration repeats a known number of times.
- Use
forwithrange()for numeric counts. - The stop value in
range()is not included.