04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The items array has 50 places reserved in memory for ints. The itemNames array has<br />

10,000 places reserved in memory for Strings, <strong>and</strong> the obs array has 0 spaces in memory<br />

for Objects. In general, you wouldn’t initialize an array with 0 items, but you could. Finally,<br />

there is one additional way to initialize an array, when you know ahead of time what will<br />

be in it:<br />

int[] speeds = {2, 4, 445, -120, 3, 90, 54};<br />

This array will have a length of 7, <strong>and</strong>, like all arrays, its size can’t be changed. Here’s<br />

another example with a String array:<br />

String[] names = {"Lulu", "Ivan", "Myrna", "Pookie"};<br />

So, once you declare <strong>and</strong> initialize arrays, how do you use them? An index system is used<br />

to access the values (called elements) stored at the different positions in the array. It’s a<br />

very simple system with one caveat: arrays are zero-indexed, which means the first position<br />

in the array is at position 0, not 1. There are some benefits to this, which you’ll see<br />

shortly when we get to for loops, but in the beginning it can be confusing.<br />

Going back to the previous array, int[]speeds = {2, 4, 445, -120, 3, 90, 54}, to<br />

access the first item—the value 2—I use speeds[0]; to access the third item, 445, I use<br />

speeds[2]; <strong>and</strong> for the last item, 54, I use speeds[6]. Make sure you underst<strong>and</strong> this<br />

before you move on; it’s important. Arrays also have a built-in length property that you’ll<br />

use often. You access the property with dot syntax. To find the length of the speeds array,<br />

you write speeds.length.<br />

The length of the array is the actual number of items in the array, so speeds.length is<br />

equal to 7, even though speeds[6] gives you the last item in the array. You should see how<br />

working with arrays is both simple <strong>and</strong> a little complicated.<br />

Finally, if you try to access an index that is out of range—for example, if you try to access<br />

speeds[7], which doesn’t exist—you’ll get a compiler error. This may seem like a pain, but<br />

again it’s there to help you.<br />

Loops<br />

One of the best uses for computers is h<strong>and</strong>ling repetitive, redundant tasks that are too<br />

tedious for most people. This is accomplished in programming by using structures called<br />

loops. Loops are structures that continue to run, or execute, until some condition is met<br />

that causes them to stop. The two types of loops listed in the <strong>Processing</strong> language reference<br />

are while loops <strong>and</strong> for loops. In addition, we’ll look at one other variation on the<br />

while loop called a do...while loop.<br />

while<br />

Here’s an example of a while loop, (which I don’t recommend you run):<br />

while (true) {<br />

println ("help I'm in an infinite loop");<br />

}<br />

CODE GRAMMAR 101<br />

85<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!