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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

74<br />

With regard to the last two operators (== <strong>and</strong> !=), the double-equal sign (==) tests for<br />

equality, <strong>and</strong> the symbols != test for inequality; therefore, in the following (non-code)<br />

lines:<br />

if(3==3) would be true.<br />

if(3!=3) would be false.<br />

if(3!=5) would be true.<br />

Conditional operators<br />

The relational operators are often used in conjunction with two conditional operators for<br />

creating somewhat more complex conditions to evaluate. The two operators are<br />

&& (logical <strong>and</strong>)<br />

|| (logical or)<br />

One minor warning: The operator terms relational <strong>and</strong> conditional (<strong>and</strong> also logical) are<br />

used inconsistently, especially around the Web. I’m classifying the operators as specified<br />

on Sun’s site. See http://java.sun.com/docs/books/tutorial/java/nuts<strong>and</strong>bolts/<br />

opsummary.html for more info.<br />

Following are some examples. To check if x is greater than 3 <strong>and</strong> less than 10:<br />

if (x > 3 && x < 10) {<br />

//code in here would only<br />

//execute when condition is true<br />

}<br />

To check if x is less than 20 or greater than 50:<br />

if (x < 20 || x > 50) {<br />

//code in here would only execute<br />

//when condition is true<br />

}<br />

To check if the boolean variable myLecturesAreBoring is not true:<br />

if (!myLecturesAreBoring) {<br />

//code in here would only execute when<br />

//condition is true-which is always!!<br />

}<br />

By putting the exclamation symbol in front of myLecturesAreBoring, you are actually<br />

checking for the value to be false.<br />

The relational <strong>and</strong> conditional operators are used to control the logical flow of your program.<br />

In addition to using the keyword if, you can combine it with the keyword else.<br />

These types of statements, which control the branching logic in your programs, are called<br />

conditional statements. I’ll discuss them in more detail a bit later in this chapter.

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

Saved successfully!

Ooh no, something went wrong!