
This week we looked at two important concepts, loops and lists.
Loops
Start by imagining we wanted to draw four circles next to each other. We could write four calls the ellipse function, like this:
let size = 50;
ellipse(25, 25, size, size);
ellipse(75, 25, size, size);
ellipse(125, 25, size, size);
ellipse(175, 25, size, size);
Pretty easy. What if we had variables for the positions? We’d get this:
let size = 50;
let x = size / 2;
ley y = size / 2;
ellipse(x, y, size, size);
x = x + size;
ellipse(x, y, size, size);
x = x + size;
ellipse(x, y, size, size);
x = x + size;
ellipse(x, y, size, size);
x = x + size;
It’s longer than before, but notice how the same two lines now keep repeating. If we had a way to say “do these two lines four times” then this would get much shorter, and we do. We use the for statement:
let size = 50;
let x = size / 2;
ley y = size / 2;
for (let i = 0; i < 4; i++){
ellipse(x, y, size, size);
x = x + size;
}
The for statement is a bit complicated, so let’s break it down:
for (do first; check to see if we keep going; do every time 2) {
do every time 1
}
Note the curly brackets (braces) containing the stuff we want repeated.
So in our case we:
- First create a new variable called i and give it the value 0
- Check to make sure that i is less than 4
- Draw our ellipse and make x bigger
- Increase i by 1
- Go back to step 2 and check if we can keep going, otherwise stop
This means that i will have the values 0, 1, 2 and 3 and our two lines will be run four times in total. Result! Our code can draw a row of circles. If we increase the value in the check, we can have as many as we like. We choose to have 8.
Nested Loops
Nesting a loop means putting one loop inside another. What’s the point of that? Well in our case we have a loop that can draw a single row of circles. If we put all of that inside another loop we could draw several rows. The row outside has a different variable j and also runs eight times. After we draw our row we do two things:
- We make y bigger move down the screen
- We move x back to the left to its starting position for the next row
The code now looks like this:
let size = 50;
let x = size / 2;
let y = size / 2;
for (let j = 0; j < 8; j++){
for (let i = 0; i < 8; i++){
ellipse(x, y, size, size);
x = x + size;
}
y = y + size; // Move down and
x = size / 2; // Back to the left
}
We now have 8 x 8 = 64 circles in a grid.
Changing the Colour of Some Circles Based on a Check
We then added code just before our call to ellipse() to change the colour based on some check:
if( /* some check here */ ){
fill("red");
}
else {
fill("white");
}
We experimented with some different checks to see what might happen. This check turns the upper-left of the grid red:
if (i + j < 8 ){
: : :
This check, using the modulus operator, turns every third circle of the grid red:
if ((i + j) % 3 == 0){
: : :
This check paints a red cross through the centre of the grid:
if (i == 3 || i == 4 || j == 3 || j == 4){
: : :
Lists
We then jumped quickly into lists. Also called arrays, lists are like a variable that can store several values. We create them simply like this:
let a = []; // An empty list
let b = [1, 2, 4, 7]; // A list created with four entries
To get at the entries in a list you just use the list variable’s name and square brackets containing the number of the entry in the list you want to get out, noting that the first one is zero:
b[0] = 10; // Set the first entry in the list to ten
b[4] = 301; // Set the fifth entry in the list to three hundred and one
A list isn’t just for holding numbers though, it can hold anything. It can hold strings for example, or even other lists!
It’s this idea of holding lists that we use to define our pattern.
Defining our Pattern
We make a list with eight entries, each of which is a list also containing eight entries, all zero. We write it so it forms a neat block like this:
let pattern = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];
Then down in our code, where we’re deciding what colour to make our circles, we change the check to read:
if (pattern[j][i] == 1){
: : :
What does this mean? Well, j is a counter that goes from 0 -> 7 as we go down our eight rows. Given that, pattern[j] means get entry from our list for that row. Since pattern[j] is a list too, we need to say which entry we want in. The variable i goes from 0 -> 7 as we go across each row. So, pattern[j][i] gets the list for the row and then picks out the number for that column.
Once it’s set up, we can then change zeros to one in our pattern and have our circles turn red to match (red in the text below to make them stand out):
let pattern = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];

We have essentially built something that works like an image file! Our pattern is the image data and our program is the code that draws it.
As several smart people already noticed, you’re not limited to one and zero. By using more numbers, and expanding the if statement, you can have as many colours as you like.
Download
The files for this week are on our GitHub, as always.