Counting loops
After this: Count from one number up to another, and say why the end number is left out.
On this page · 6 sections · 3 checks
Print the numbers from 1 to 5. With the last lesson you could write the row [1, 2, 3, 4, 5]{1, 2, 3, 4, 5} and loop over it. Now print the numbers from 1 to 100. Typing out a row of a hundred numbers is the kind of work a loop was meant to save you. Counting is so common that both languages have a short way to say "the numbers from here up to there".
From a start number up to an end number
Work through the loop below, which prints i on every pass.
iis 1. The count has not reached 6, so the print line runs: 1.ibecomes 2, and the print line runs again: 2. Then 3, then 4, then 5, the same way.iwould be 6 next. 6 is the end number, so there is no sixth pass. The program moves on and prints Done.
- Run the programRun it and count the passes. The end number is 6; watch whether 6 is ever printed.
for i in range(1, 6): print(i)print("Done")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Five numbers, 1 to 5. The number 6 was the stop sign, not a value that was printed.
Stopping one short
This is the single most common slip a beginner makes with loops: writing 5 as the end number to count to 5, and getting 1 to 4. The count stops before the end number. To count to 5, the end number is 6.
| You write | i takes | Passes |
|---|---|---|
| range(1, 6)i = 1; i < 6 | 1 2 3 4 5 | five |
| range(1, 5)i = 1; i < 5 | 1 2 3 4 | four |
| range(0, 3)i = 0; i < 3 | 0 1 2 | three |
A way to check yourself: the number of passes is the end number minus the start number. 6 minus 1 is five passes.
The counter is a box like any other
Inside the loop, i can be used wherever a value can. This program adds the numbers 1 to 4. total goes 0, 1, 3, 6, 10.
total = 0for i in range(1, 5): total = total + iprint(total)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The last pass adds 4; the end number, 5, is never added.
Run it
Change 6 to 11 and run again. Then make the count start at 3.
Check yourself
A counting loop is written for i in range(1, 6):for (int i = 1; i < 6; i++) { and prints i on every pass. Which number is printed last?
What does this print?
for i in range(3, 6): print(i * i)for (int i = 3; i < 6; i++) { System.out.println(i * i);}Complete the loop line so that the program prints 1, 2, 3 and 4, each on its own line. Write the whole line, including the colon at the endbrackets and the opening brace.
____________________ # ← the loop line: 1 up to 4 print(i)____________________ // ← the loop line: 1 up to 4 System.out.println(i);}Honesty notes
- Write a counting loop from a start number up to an end number (range in Python; a three-part for in Java).
- Say that the end number is left out: 1 up to 6 gives 1, 2, 3, 4, 5, and the number of passes is end minus start.
- Use the counter inside the loop, as any other box: adding 1 to 4 gives 10.
- Avoid the most common slip: to count to 5, the end number is 6.