Loops 🩵 Arrays#
With arrays and their numeric indexes, I don't think it will be a huge surprise that we can easily combine arrays and loops. For this, by far the most common loop is the for
-loop. Let's say we have this code here:
Multiple Lines
Yes, you can write an array over multiple lines. I would suggest indenting the entries by one level. This still is not "a block of code", it's still just a list of strings that we're assigning to our String array variable.
How would we now work with that? Of course, we can still use movies[3]
to get to the value "Thor", but let's put this into a loop instead!
continuation! we're assuming the array above! | |
---|---|
This will print the first three entries from that array. We're just using a for
-loop to give us numbers from 0 to 2 in the variable i
, we then put that variable i
into the square brackets to access the value at that index!
The Array's Length#
That for
-loop is pretty interesting and useful already, but that array six entries and not just three. How do we print all of them? And what would happen if I removed or added a few entries? This looks terribly brittle1! Whenever I change my array, I now need to also change that for loop? Isn't there a better way?
Yes there is! An array knows how many entries it holds! It knows what we call its "length". Let's print that length:
continuation! we're assuming the array above! | |
---|---|
With that new piece of information, can you change the for
loop, to run for the correct number of times?
What would the for
-loop look like to print all the entries?
continuation! we're assuming the array above! | |
---|---|
Yes, we just need to replace our literal value 3
in the for loop with our movies.length
which will always be the correct number, even if we change the array later on!
Did you get that right?
Properties
We call .length
a "property" of the array. This behaves pretty much exactly like a variable would and you can use it in the exact same places.
You have seen something similar with the mouse wheel event, where we had something similar to a function. That .
dot notation will be explained next week in more detail.
Example: Oh Snap!#
Let's start with a more involved example that actually does something. To get there, please take a good look at this piece of code (no, it does not contain an array, yet)!
We draw a circle at the mouse coordinates, but if the mouseX
is close to our snapAnchor
(line 13), then we snap our circle to its coordinates. Code very similar to this one will be in all the graphical tools you use, this is basically how all guidelines work! Make sure you understand the code above before moving on.
Your graphical tool, though lets you have more than one single guideline, so how do we do that? We don't want to litter our global variables with stuff like this?
float snapAnchor1 = 150;
float snapAnchor2 = 200;
float snapAnchor3 = 80;
// ... and so on ...
Instead we want to have a variable that can contain "many" things at once. An Array. This following code is very similar to the code above – in fact, it only differs in three lines (marked below)!
So, in this example, we get to define as many guidelines to snap to as we want!
Change the Guidelines
Copy the Code from above to your Processing Editor. Change the numbers, add or remove a few numbers on line 1 of the code. Does it still run with more (or fewer) guidelines in place?
Relevant excerpt from Learning Processing#
(the section starts at 3:28:02 and runs through 3:40:29, the video should start and stop on these automatically.)
-
When code breaks easily because of small changes like that, we call that "brittle". Changing a list like that should usually not break the rest of the program. ↩