Nested Loops — Pseudocode

When a loop is inside a loop, the inner loop runs to its end for each round of the outer loop. The numbers of rounds are multiplied: 3 rows of seats times 4…

Before we start, guess: how many lines does this program print, and what is on the fourth line?

FOR row ← 1 TO 2
    FOR seat ← 1 TO 3
        PRINT "Row", row, "seat", seat
    END FOR
END FOR

This program has a FOR inside a FOR. The one on the outside is called the outer loop (its variable is row, from 1 to 2). The one inside it is called the inner loop (its variable is seat, from 1 to 3).

The inner one finishes first, then the outer one moves on

This is what the program above prints:

Row 1 seat 1
Row 1 seat 2
Row 1 seat 3
Row 2 seat 1
Row 2 seat 2
Row 2 seat 3

Six lines are printed, and the fourth is Row 2 seat 1. While row still holds 1, the inner loop runs to its end: seat holds 1, 2, then 3. Only then does row go up to 2, and the inner loop starts again from seat 1. So the PRINT line runs 2 times 3, which is 6 times.

The order is the same as reading a seating plan: finish one row of seats from left to right, then go down to the next row. One row of seats is one round of the outer loop, and each seat is one round of the inner loop.

One row of the plan, put together first

The neighbourhood head wants a seating plan for watching a match together in the village hall: three rows, four seats in each row, with O for a free seat. The problem is that each PRINT starts a new line. Four PRINT "O" lines give four lines, not OOOO. So the program first builds one row of seats as one text in the variable line, then prints it. The result is below the code:

line ← ""
FOR seat ← 1 TO 4
    line ← line + "O"
END FOR
PRINT line
OOOO

"" is empty text: text with no letters in it yet. In each round, line + "O" joins one O onto the end of line, so line holds O, OO, OOO, then OOOO. PRINT line is outside the loop, so it runs only once, when the row of seats is complete.

The whole plan

For three rows of seats, the loop that builds one row goes inside another loop:

FOR row ← 1 TO 3
    line ← ""
    FOR seat ← 1 TO 4
        line ← line + "O"
    END FOR
    PRINT line
END FOR
OOOO
OOOO
OOOO

Look at where these three lines of code are:

You can mark a seat that is already booked with X, using an IF inside the inner loop. In this program, seat 3 in row 2 is booked:

FOR row ← 1 TO 3
    line ← ""
    FOR seat ← 1 TO 4
        IF row = 2 AND seat = 3 THEN
            line ← line + "X"
        ELSE
            line ← line + "O"
        END IF
    END FOR
    PRINT line
END FOR
OOOO
OOXO
OOOO

An inner limit that follows the row number

The limit of the inner loop (its end number) may use the outer loop's variable. In this program the limit is 5 - row, so each row of seats is shorter than the row above it:

FOR row ← 1 TO 4
    line ← ""
    FOR seat ← 1 TO 5 - row
        line ← line + "O"
    END FOR
    PRINT line
END FOR
OOOO
OOO
OO
O

When row holds 1, 5 - row is 4, so the inner loop runs 4 times. When row holds 4, 5 - row is only 1. Triangle shapes can be made this way.

⚠️ A plan that spreads, piles up, or gets cut short

TL;DR: When a loop is inside a loop, the inner loop runs to its end for each round of the outer loop. The numbers of rounds are multiplied: 3 rows of seats times 4 seats, 12 rounds. Each PRINT starts a new line. So to print one line that holds many marks, first build the marks into one text (line ← "" for empty text, then line ← line + "O" in each round), then PRINT once per row. Empty the text at the start of each row. The limit of the inner loop may use the outer loop's variable, and that is how you make triangle shapes. The outer loop and the inner loop need two different variable names.

⚡ The inner one finishes first

Write:

FOR row ← 1 TO 2
    FOR seat ← 1 TO 3
        PRINT "Row", row, "seat", seat
    END FOR
END FOR
Guess first: how many lines are printed, and what's the fourth one?

Six lines, and the fourth is Row 2 seat 1. While row holds 1, seat goes through 1, 2 and 3 first. Only then does row go up to 2.

Meaning: The FOR on the outside is called the outer loop, and the FOR inside it the inner loop. In each round of the outer loop, the inner loop runs to its end, then starts again from the beginning in the next round. The numbers of rounds are multiplied: 2 times 3, so 6.

⚡ The whole plan

Write:

FOR row ← 1 TO 3
    line ← ""
    FOR seat ← 1 TO 4
        line ← line + "O"
    END FOR
    PRINT line
END FOR
Guess first: if line ← "" (it fills line with empty text) is moved to the top, outside both loops, what gets printed?

OOOO, OOOOOOOO, then OOOOOOOOOOOO. line is never emptied again, so each row of seats continues from the row before.

Meaning: This code prints a plan of 3 rows of seats, each row OOOO. Each PRINT starts a new line, so one row of seats is first built as text in line. line ← "" empties line at the start of each row of seats, line ← line + "O" adds one O for each seat, and PRINT line runs when the inner loop is done.

⚡ Two loops, two names

Meaning: If the outer loop and the inner loop use the same variable name, for example both are FOR i, the inner loop also changes the i that the outer loop uses. The 3-row plan above then prints only one row, with no error. Use two different names, like row and seat.

What you will learn