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.

In the first preceding example, addition <strong>and</strong> subtraction have the same precedence level,<br />

so the right side of the assignment statement is evaluated from left to right. (The assignment<br />

itself still occurs from right to left.)<br />

The second example combines multiplication, division, <strong>and</strong> subtraction. Multiplication <strong>and</strong><br />

division operations occur before subtraction. Since multiplication <strong>and</strong> division are at the<br />

same precedence level, this expression is evaluated left to right.<br />

The last example uses the same oper<strong>and</strong>s <strong>and</strong> operators as the second example, but uses<br />

parentheses to group. Since parentheses have the highest precedence, the subtraction in<br />

the parentheses happens first, <strong>and</strong> then the multiplication/division occurs from left to right.<br />

I’ve covered all the arithmetic operators but one: %, called the modulus operator. When I<br />

first learned about this operator, I found it confusing <strong>and</strong> a little scary looking <strong>and</strong> sounding.<br />

It’s actually not such a big deal. The modulus operator doesn’t mean percent, but<br />

rather returns the remainder of the division between two oper<strong>and</strong>s. The remainder is the<br />

part left over after the division.<br />

For example, 7 % 2 would evaluate to 1, since 2 goes into 7 three times, leaving a remainder<br />

of 1. Here are some more examples:<br />

17 % 9 evaluates to 8.<br />

6 % 3 evaluates to 0.<br />

23.4567 % 5 evaluates to 3.4567.<br />

In addition to the five arithmetic operators you just looked at, there are quite a few others.<br />

However, you only need to concern yourself with the most common ones for now.<br />

These operators can be broken down into three categories: the equality <strong>and</strong> relational<br />

operators, the conditional (also sometimes referred to as logical) operators, <strong>and</strong> the<br />

assignment operators.<br />

Relational operators<br />

The relational operators are very important, <strong>and</strong> you will use them often. Most of these<br />

operators should look familiar to you from grade school (although probably not the last two):<br />

> (greater than)<br />

>= (greater than or equal to)<br />

< (less than)<br />

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

Saved successfully!

Ooh no, something went wrong!