Counting Loops — Pseudocode

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…

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

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?

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.

What you will learn