Lists & For Each

One is better than many but never quite enough

Until now, every variable has held a single value. A list lets one variable hold many values at once, kept in order. Lists are useful any time we have a collection of related things, like a set of scores, a group of names, or a series of measurements.

List (noun)
An ordered set of values

Lists

We create a list by placing values inside square brackets, separated by commas.

csp
You may also type <- !=>= <= for auto complete.

A list is a value just like a string, number, or boolean, so we can store it in a variable, as we did above.

A list cannot be displayed directly. DISPLAY(scores) is an error. To display the contents of a list, we must go through it one item at a time, which is what the FOR EACH loop later in this chapter is for.

csp
You may also type <- !=>= <= for auto complete.

Mixing Types

A list does not care what kind of values it holds. A single list can contain strings, numbers, and booleans all at once, in any combination.

csp
You may also type <- !=>= <= for auto complete.

This list holds a number, a string, another number, a boolean, and a string that only looks like a boolean. Each item keeps its own type. The "false" at the end is text, not the boolean false, because it is wrapped in quotes.

Mixing types is allowed, but it is up to you to keep track of what is in your list. Adding 1 to a number makes sense, but adding 1 to "two" does not. Most of the time, it is best to keep the items in one list the same type.

Expressions and Variables in Lists

Each item in a list can be any expression, not just a plain value. CSP evaluates each expression and stores the result.

csp
You may also type <- !=>= <= for auto complete.

This list holds the values 10, 20, 30, 279, and the string "3423". The variables and the addition are evaluated at the moment the list is created. The list stores the results, not the names or the arithmetic.

Notice that 279 and "3423" are different types. The first is a number, but the second is a string that happens to contain digits.

For Each

Once we have a list, we often want to do something with every item in it. A FOR EACH loop does exactly this. It runs a block of code once for every item in the list, from the first item to the last.

csp
You may also type <- !=>= <= for auto complete.

The loop has three parts. After FOR EACH comes a name that we choose, called the loop variable. After IN comes the list we want to go through. The curly braces hold the code to run each time.

On the first time through, x holds the first item in the list. On the second time, x holds the second item, and so on. Each time through the loop is called an iteration.

Iteration (noun)
A single pass of a loop

Since a whole list cannot be displayed, a FOR EACH loop is how we display one. Each item on its own can be displayed.

csp
You may also type <- !=>= <= for auto complete.

This runs three times, displaying red, then green, then blue, each on its own line.

IN must be written in all uppercase letters, like every other CSP keyword.

The name of the loop variable is entirely up to us. It is a good habit to name the list with a plural word and the loop variable with the matching singular word, like colors and color. That way the loop reads almost like English: “for each color in colors.”

Loops and Mixed Types

Because a list can mix types, the loop variable can hold a different type on each iteration. The loop does not care. It simply hands over each item in turn.

csp
You may also type <- !=>= <= for auto complete.

Every individual item can be displayed, no matter its type, so this program is fine. Problems appear when the body of the loop assumes a type. Here, the loop adds 1 to every item.

csp
You may also type <- !=>= <= for auto complete.

This works for 1 and 3.5, but "two" and true are not numbers, so adding to them does not make sense. When looping over a list, always be sure of what kinds of values it contains.

Using Conditionals in a Loop

A loop body can contain anything, including an IF. This lets us treat items differently depending on their value.

csp
You may also type <- !=>= <= for auto complete.

Each number is tested in turn, so this displays even, odd, even, odd.

A loop variable can also be combined with an ordinary variable to build up a result. This program adds up every number in a list.

csp
You may also type <- !=>= <= for auto complete.

Before the loop, total is 0. Each iteration adds the current item to it, so total becomes 4, then 11, then 21, and finally 34. Because the DISPLAY is after the closing brace, it runs once, after the loop has finished.


Next: List Functions & Indexing