Dodi's homework tonight: the 7 times table, from 7 x 1 to 7 x 10. With what you have learned so far, the program looks like this:
PRINT "7 x 1 = 7"
PRINT "7 x 2 = 14"
PRINT "7 x 3 = 21"
// ... and seven more lines, up to 7 x 10
That is ten lines that are almost the same. Tomorrow the homework is the 8 times table, and you have to type ten more lines. If you mistype one number, nobody notices.
But only one number changes from line to line: 1, 2, 3, up to 10. The answer on each line comes from that number. The computer can count from 1 to 10 itself, then repeat the same line for each number. A command that makes the computer repeat lines is called a loop.
FOR ... TO ... END FOR
This loop prints the numbers 1 to 5. The result is below the code:
FOR i ← 1 TO 5
PRINT i
END FOR
1
2
3
4
5
i ← 1is the start: the variableibegins at 1.TO 5is the end, and 5 itself is included.- The lines between
FORandEND FORrun once for each value ofi. One run through them is called one round. - After each round,
igoes up by one. As soon asiis more than 5, the loop is done, and the program continues with the line afterEND FOR. - The variable name is up to you.
iis only a habit among programmers.glass,dayornumberwork too.
The body can use the number
The lines inside a loop (its body) can use the loop's variable. In this program, the PRINT line uses glass:
FOR glass ← 1 TO 3
PRINT "Glass", glass, "ready"
END FOR
PRINT "Order complete"
Glass 1 ready
Glass 2 ready
Glass 3 ready
Order complete
The PRINT line is the same, but the number is different in each round. PRINT "Order complete" is below END FOR, so it is outside the loop. It runs only once, after every round is done.
Before you read on, guess: if the first line becomes FOR glass ← 0 TO 3, how many glasses are printed? Four: glasses 0, 1, 2 and 3. The start number and the end number are both included.
In JavaScript later, the loop from 1 to 3 is written for (let i = 1; i <= 3; i++). In Python it is written for i in range(1, 4). range stops before its second number, so in Python the end is written as 4, not 3.
STEP: jumping, or going backwards
STEP goes at the end of the FOR line and sets how much the variable changes in each round:
FOR number ← 2 TO 10 STEP 2
PRINT number
END FOR
2
4
6
8
10
STEP 2 makes number go up two at a time. To count down, the step is negative. STEP -1 makes seconds go down by one in each round:
FOR seconds ← 3 TO 1 STEP -1
PRINT seconds
END FOR
PRINT "Courier robot sets off"
3
2
1
Courier robot sets off
Adding up while repeating
Dodi saves money every day. He saves $1 on the first day, $2 on the second, and one dollar more each day until the seventh. How much has he saved after a week? This program adds it up:
total ← 0
FOR day ← 1 TO 7
total ← total + day * 1
END FOR
PRINT "A week's savings:", total
A week's savings: 28
total ← 0 is written once, above the loop. In each round, that day's savings, day * 1, are added to total. The PRINT line is below END FOR, so only the final result is printed.
⚠️ Why are there too few rounds, too many, or none?
END FORis missing. The program stops with the message "Line 1: this FOR is never closed." The next line of the message says where: "Add END FOR after line 2."- The wrong closing line. If you close a
FORwithEND IF, the program stops with the message "Line 3: END IF closes the FOR from line 1." AFORis closed withEND FOR. - A word the computer does not know.
FOR glass ← 1 THROUGH 3stops with the message "Line 1: this loop line is not complete, there isTHROUGH." The right word isTO. total ← 0is missing. In the first round,total ← total + day * 1stops with the message "Line 2:totalis used before it has a value." If you puttotal ← 0inside the loop instead, there is no error. Buttotalis set back to 0 in every round, and the result is only 7: the seventh day's savings alone.- Counting down without
STEP -1.FOR seconds ← 3 TO 1gives no error, but it runs 0 times: withoutSTEPthe number goes up, and the start (3) is already past the end (1). The Playground (where you run your own pseudocode) adds a note: "Line 1: this FOR runs 0 times, because3is bigger than1."
TL;DR: FOR i ← 1 TO 10 ... END FOR is a loop: the lines inside it run once for each value of i, from 1 to 10, and 10 is included. Inside the loop, you can use i like any variable. STEP 2 at the end of the FOR line makes i go up two at a time, and STEP -1 makes i go down by one (counting down). A line that should run only once goes outside the loop, for example total ← 0 before adding up, or the PRINT of the final result.
⚡ One line, repeated for each number
Write:
FOR i ← 1 TO 5
PRINT i
END FOR
Meaning: This is a loop: the lines between FOR and END FOR run once for each value of i, so it prints 1 to 5. i ← 1 is the start, TO 5 is the end, and 5 itself is included. One run through the lines is called one round. After each round, i goes up by one.
⚡ The start and the end both count
Write:
FOR glass ← 1 TO 3
PRINT "Glass", glass, "ready"
END FOR
PRINT "Order complete"
Guess first: if the first line is FOR glass ← 0 TO 3, how many glasses are printed?
Four: glasses 0, 1, 2 and 3. The start number and the end number are both included.
Meaning: PRINT "Order complete" is below END FOR, outside the loop, so it runs only once, after every round is done.
⚡ STEP: jumping or going backwards
Guess first: how many times does FOR seconds ← 3 TO 1 run, with no STEP?
0 times, with no error. Without STEP the number goes up, and the start (3) is already past the end (1).
Meaning: STEP at the end of the FOR line sets how much the variable changes. STEP 2 makes the number go up two at a time. To count down, the step is negative: FOR seconds ← 3 TO 1 STEP -1.
⚡ Adding up while repeating
Write:
total ← 0
FOR day ← 1 TO 7
total ← total + day * 1
END FOR
PRINT "A week's savings:", total
Meaning: Each round adds day * 1 to total: 1, 2, up to 7. The final result is 28. total ← 0 is written once, above the loop. If you put it inside, total is set back to 0 in every round, and the result is only 7.