Nested Loops — Pseudocode

Before we start, a guess. How many lines does this program print, and what is the fourth one? FOR row ← 1 TO 2 FOR seat ← 1 TO 3 PRINT "Row", row, "seat"…

Before we start, a guess. How many lines does this program print, and what is the fourth one?

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

There is a FOR inside a FOR. The outer one runs 2 times, the inner one 3 times. The answer is below.

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

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, and the fourth is Row 2 seat 1. While row is still 1, the inner loop runs all the way: seat 1, 2, 3. Only then does row go up to 2, and the inner loop starts again from seat 1. The inner rounds add up to 2 times 3, which is 6.

Just like reading a seating plan: finish one row from left to right, then go down to the next row.

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, O for a free seat. The trouble is, each PRINT always prints a line of its own. Four PRINT "O" give four lines, not OOOO. So the row is put together as one text first, then printed:

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

"" is empty text, with no letters yet. Each round, line + "O" joins one O onto the end of line: O, OO, OOO, OOOO. The PRINT is outside the loop, so it prints once, when the row is complete.

The whole plan

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 the three places:

A seat that is already booked can be marked X with an IF inside the inner loop. Here 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 inner loop's limit may use the outer loop's variable. Here the rows get shorter going down:

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

In row 1, 5 - row is 4, so the inner loop runs 4 times. In row 4, only 1 is left. Triangle shapes always work like this: how many times the inner loop runs depends on the row number.

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

TL;DR: A loop inside a loop runs the inner one all the way for each round of the outer one, so the total is outer times inner: 3 rows times 4 seats, 12 rounds. To print one line holding many marks, build it as text first (line ← "", then line ← line + "O"), then PRINT once per row. Empty the text at the start of each row. The inner loop's limit may use the outer one's variable, and that is where triangle shapes come from. Use two different names for two loops.

What you will learn