A Box That Holds Many
A list is one name holding a row of values, and a loop can walk along it.
Every box so far holds one value: a score, a guess, a name. Plenty of things come in rows, though. Five high scores. Four lives. A screen full of enemies.
A list is one name holding many values, in order.
Making a list
LET scores = [10, 20, 30]
SHOW scores[1]
10
The square brackets are doing two different jobs on those two lines, so it is worth
separating them. On the first, [10, 20, 30] makes a list — the brackets hold the
values you are putting in it. On the second, scores[1] reaches into a list you
already have, and the number in the brackets says which item you want.
scores[1] means the first item in scores. Counting starts at 1 here: the first item
is item 1, the second is item 2.
Not every language counts from 1. Many start at 0, which makes the first item item 0. It is one of the first things worth checking when you pick a language up.
Changing and growing
LET scores = [10, 20, 30]
LET scores[2] = 99
APPEND 40 TO scores
SHOW scores[2], " and ", scores[4]
99 and 40
LET scores[2] = 99 puts a new value in the second place, the same way LET fills any
other box. APPEND adds a value to the end, and the list grows to hold it.
LENGTH(scores) says how many items a list holds — the round brackets hand it the list
to count. After that APPEND, it is 4.
Walking the list
A list and a loop belong together. FOR EACH runs the work once for every item:
LET scores = [10, 99, 30, 40]
FOR EACH score IN scores
SHOW score
END
10 99 30 40
You never had to say how long the list was. FOR EACH runs once per item, however many
there are, so those three lines work for four scores or four hundred.
A loop inside a loop
Put a loop inside another one and you get a grid. The inner loop runs all the way through for every single pass of the outer one:
FOR row = 1 TO 3
FOR column = 1 TO 3
SHOW "row ", row, " column ", column
END
END
row 1 column 1 row 1 column 2 row 1 column 3 row 2 column 1 row 2 column 2 row 2 column 3 row 3 column 1 row 3 column 2 row 3 column 3
Nine lines, not six: three columns for each of three rows. Nesting is how you fill a screen, draw a board or walk a level, and the count multiplies rather than adds.
When it’s wrong, see why
- You ask for an item that is not there.
scores[9]in a list of four has nothing to give you. Check the length before reaching past the end. - Everything is out by one. You counted from 0 out of habit, or from 1 in a language that counts from 0. Check which one you are in.
- Nothing happens at all. The list is empty.
FOR EACHover an empty list runs no times, which looks the same as a program that did nothing.
What you’ve learnt
- A list is one name holding many values, in order.
scores[2]reaches one item, counting from 1 here and from 0 in many languages.APPENDgrows a list, andLENGTHsays how many items it holds.FOR EACHruns work once per item, without you knowing how many there are.
What’s next
Your programs are getting longer, and the same little job keeps turning up in more than one place. In Unit 2 you give a job a name and write it once.