Doing it again
After this: Run the same lines once for each value in a row, and follow the boxes pass by pass.
On this page · 5 sections · 3 checks
Suppose you want to print three numbers, 3, 1 and 4, one under another. You could write three print lines. Now suppose you want to print a hundred numbers. A hundred print lines is a lot of typing, and when you decide to print them differently, you have to change all hundred.
Every program you have written so far ran each line once, from top to bottom. A decision could skip some lines, but no line ever ran twice. This lesson adds the missing piece: a way to run the same lines again and again.
Write the line once, run it three times
The idea is to write the print line once and tell the computer: run this line once for each of these values. The three values are written as a row, [3, 1, 4]new int[]{3, 1, 4}, and a box, n, takes each value in turn.
The smallest version is in the trace below. Read its code, then work through it one step at a time.
- The first line names the box
nand the row of values. The computer puts the first value, 3, inton. - The line under it belongs to the loop. It runs and prints 3.
- The computer goes back to the first line. It puts the next value, 1, into
n, and the print line runs again: 1. - The same once more with 4.
- Nothing is left. The computer moves on to the line after the loop and prints Done.
A set of lines that the computer runs again and again is a . Each run through those lines is one . The loop above makes three passes, one per value.
for n in [3, 1, 4]: print(n)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
The trace narrates every jump back to the top as "The loop goes back to line 13", and the end as "Nothing is left to loop over". Step through and count three of the first and one of the second.
Adding up as you go
A loop can do more than print. The program below adds the three values into a box named total. Before the loop, total holds 0.
| Pass | n | total after the pass |
|---|---|---|
| 1 | 3 | 0 + 3 = 3 |
| 2 | 1 | 3 + 1 = 4 |
| 3 | 4 | 4 + 4 = 8 |
The line total = total + n runs three times, and each time it adds the current value of n to the box. After the loop, total holds 8, and the last line prints it. Watch the two boxes in the trace: n takes a new value at the start of each pass, and total grows at the end of each pass.
- Run the programRun it and step through. Watch n take a new value at the start of each pass, and total grow at the end of each pass.
total = 0for n in [3, 1, 4]: total = total + nprint(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 rule: a loop over a row of values runs its lines once for each value, from the first value to the last, and the box holds the current value during each pass. When the values are used up, the program carries on with the line below the loop.
Run it
Python runs in the browser when you press Run below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Change the values, or add a fourth one, and run again: the total follows.
Check yourself
A loop goes through the row [3, 1, 4]{3, 1, 4} and has one print line inside it. How many times does that print line run?
After the second pass of this loop, what do total and n hold?
total = 0for n in [3, 1, 4]: total = total + nprint(total)int total = 0;for (int n : new int[]{3, 1, 4}) { total = total + n;}System.out.println(total);What does this print?
for n in [2, 5]: print(n * 10)for (int n : new int[]{2, 5}) { System.out.println(n * 10);}Honesty notes
- Say what a loop is: lines the computer runs again and again, once per value in a row.
- Read a for loop: the box takes each value in turn, from the first to the last, and the lines under it run once per value.
- Follow the boxes pass by pass: 0, 3, 4, 8 for a total that adds up 3, 1 and 4.
- Find the two jumps the trace narrates: the loop goes back to its first line, and nothing is left to loop over.