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:
line ← ""is inside the outer loop, before the inner one. Each row starts from empty text.line ← line + "O"is inside the inner loop. It runs once per seat, 12 times in all.PRINT lineis inside the outer loop, after the inner one. It runs once per row, when the row is complete.
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
line ← ""outside both loops. No error, butlineis never emptied again, so each row carries on from the one before:OOOO,OOOOOOOO,OOOOOOOOOOOO.PRINT lineinside the inner loop. The row is printed every time a seat is added:O,OO,OOO,OOOO, then again for the next row. A three-row plan becomes 12 lines.- The same name for both loops. When both are
FOR i, the inner loop changes the outer loop'sitoo. The three-row plan above prints only one row, with no error. Use two different names, likerowandseat. - A number joined to text.
line ← line + seatstops with "Line 4: text can't be added to a number directly." To build text, turn the number into text first:line + TEXT(seat). - One
END FORshort. TwoFORneed twoEND FOR. When the inner one is never closed, the computer stops with "Line 1: this FOR is never closed." and then says where: "Add END FOR after line 5."
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.