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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

76<br />

These operations are so common in programming that there is a shortcut syntax more<br />

commonly used. The shortcut simply joins the mathematical <strong>and</strong> assignment operators.<br />

For example, the last expression (y = y / 1.25;) could be shortened to the following:<br />

y /= 1.25;<br />

The other mathematical operations follow the same structure. Here are some examples:<br />

float temp = 98.6;<br />

temp += 5; // temp now equals: 103.6<br />

temp -= .6; // temp now equals: 103<br />

temp *= 2; // temp now equals: 206<br />

temp %= 23; // temp now equals: 22<br />

The last expression may look odd; it’s using the modulus operator. Remember, modulus<br />

returns the remainder of division; 23 goes into 206 eight times, leaving a remainder of 22,<br />

which then gets assigned back to the temp variable.<br />

In the assignment operations you’ve been looking at thus far, the operator(s) have been<br />

surrounded by two oper<strong>and</strong>s. The geeky way to refer to these types of operators is as<br />

binary operators. <strong>Processing</strong> also uses some operators that only require one oper<strong>and</strong>,<br />

which are referred to as unary operators. Two of these very useful operators are actually<br />

shortcuts for two other shortcuts.<br />

If I want to add 1 to the int variable x using the shortcuts just shown, I could write the<br />

following:<br />

x += 1;<br />

Using a unary operator, the expression can be shortened to the following:<br />

x++;<br />

You’ll also see this expression written as ++x;. It does matter, in some contexts, on which<br />

side you put the two operators, which I’ll discuss the subtlety of later on. Besides incrementing<br />

by one, you can also decrement by one, using x-- (also --x). I recommend learning<br />

to use the shortcuts as soon as you can—they will save you keystrokes (possibly staving<br />

off carpal tunnel) <strong>and</strong> make you look like you’ve been coding for years.<br />

Conditionals<br />

Next, I want to cover conditional statements, which you got a sneak peak of when you<br />

looked at the relational <strong>and</strong> conditional operators. A conditional statement is sometimes<br />

referred to as a decision statement. Essentially, is says that if a certain condition is true,<br />

then do something; but if it is not true, do something else.<br />

I think by now you’re probably in need of some visual digression, so we’ll cover the conditionals<br />

by creating a little sketch (see Figure 3-3), which creates a bouncing ball:

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

Saved successfully!

Ooh no, something went wrong!