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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Ternary operator<br />

The last conditional structure I want to show you is Java’s ternary operator—meaning that<br />

it uses three oper<strong>and</strong>s. The ternary operator takes some time getting used to, <strong>and</strong> some<br />

people never get used to it. I tend to hardly ever use it, but other people use it a lot, so it’s<br />

worth learning. It’s logic is similar to an if...else statement. Here’s an example:<br />

//some variables<br />

int karma;<br />

boolean isStillReadingThisBook;<br />

// Here's the if/else version:<br />

if (isStillReadingThisBook){<br />

karma = 100;<br />

else {<br />

karma = 50;<br />

}<br />

// Here's the ternary operator version:<br />

karma = isStillReadingThisBook ? 100 : 50;<br />

The ternary version looks pretty terse <strong>and</strong> cryptic. Its form is really a throwback to an older<br />

style of programming. However, as you can see, it is efficient. The condition is tested, in<br />

this case the boolean variable isStillReadingThisBook; if the condition is true, then the<br />

first value after the ? is returned <strong>and</strong> assigned to the karma variable; if it is false, then<br />

the value after the : is returned. It’s really up to you whether you want to use this structure.<br />

You can always use the more verbose if...else to accomplish the same thing.<br />

Arrays <strong>and</strong> loops<br />

The next section looks at arrays <strong>and</strong> loops. I’ve grouped these somewhat large topics<br />

together because of their interconnectedness. Arrays <strong>and</strong> loops aren’t that complicated as<br />

concepts, but they can get a little hairy for newbie coders to implement. I’ll be using them<br />

to exp<strong>and</strong> on two previous sketches: the stepped radial gradient <strong>and</strong> the bouncing ball.<br />

These examples will illustrate the power of arrays <strong>and</strong> loops <strong>and</strong> also begin to show you<br />

the potential of code as a creative medium.<br />

Arrays<br />

There are times in coding when you’ll want to assign many values to a single variable. For<br />

example, maybe you have 100 balls <strong>and</strong> you want to keep track of each of their x positions.<br />

The problem with using a primitive variable is that it can only hold one value at a<br />

time. Thus, you would need 100 separate variables to account for each ball’s x position.<br />

Arrays were developed to solve this problem; they are single data structures that hold<br />

multiple values. If I wanted to animate all 100 balls, there are very easy ways to change the<br />

100 x positions stored within the array. You can think of an array as a desk with multiple<br />

drawers. On the one h<strong>and</strong>, an array is a single entity, referred to by a single name; on the<br />

other h<strong>and</strong>, the array can hold many separate values, each accessible through an indexing<br />

CODE GRAMMAR 101<br />

83<br />

3

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

Saved successfully!

Ooh no, something went wrong!