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 values in order. LET stores a value; SHOW
displays it. These examples use pseudocode, so a target language may spell the
operations differently.
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.
An index must be a whole number from 1 through LENGTH(scores). With three
items, that means 1, 2 or 3. Zero, a fraction and 4 do not select an item here.
An invalid index is outside this example’s rules; it does not mean an extra zero
value appears. A real language defines how it reports or handles the error.
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
END10 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.
An empty list
A list can hold no items:
LET scores = []
SHOW LENGTH(scores)
FOR EACH score IN scores
SHOW score
END0
The first SHOW prints zero. The loop has no item to visit, so its body never
runs. There is no scores[1] in this list.
Keep the list’s size fixed while walking it in these introductory examples. Adding or removing items during a loop needs a separate rule about which items the loop visits; do not assume every language makes the same choice.
With scores = [10], are scores[0], scores[1] and scores[2] all valid? What changes after APPEND 20?
Only scores[1] is valid at first. After appending 20, the length is 2 and scores[1] and scores[2] are valid. Index zero stays outside the range. An empty list has length zero and no valid index.
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
ENDrow 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. Inspect the list length and whether the loop was
reached. An empty list makes
FOR EACHrun zero times, but silence alone does not tell us that the list was empty.
What you’ve learnt
- A list holds values in order; valid indices run from 1 through its length.
- An empty list has no valid index, and its
FOR EACHbody runs zero times. 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.