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 />

62<br />

Here’s a conditional if...else example:<br />

if (thisBook == bestSeller) {<br />

sleepWellAtNight();<br />

} else {<br />

addMorePictures();<br />

}<br />

Here’s a function example:<br />

void sleepWellAtNight() {<br />

println("one sheep, two sheep...");<br />

}<br />

Here’s a for loop example:<br />

for (int i = 0; i < totalPictures; i++) {<br />

makeSomeInterestingPictures();<br />

}<br />

You’ll be looking at a lot more examples that make use of curly braces shortly. You’ll notice<br />

that I structured the blocks of code between the curly braces using indentations. Indenting<br />

is not necessary for the code to work, but it is the conventional way to write blocks of<br />

code. Indenting makes it easier for other coders to read your code. In the examples in this<br />

book, each indentation is created by hitting the spacebar twice.<br />

Dot syntax<br />

The last syntactical structure you’ll see in <strong>Processing</strong> is the dot (.) It looks like a period<br />

(<strong>and</strong> it technically is a period), but it’s referred to as a dot; it’s the same dot in .com. The<br />

dot is an advanced syntactic structure used in OOP.<br />

As I mentioned earlier, there are two approaches to structuring a program in <strong>Processing</strong>:<br />

procedural programming <strong>and</strong> OOP. Procedural programming uses functions to organize a<br />

program. You just saw an example of a function in sleepWellAtNight. When you need to<br />

use a function, you explicitly call it—for example, sleepWellAtNight()—pretty simple.<br />

In OOP, you build much more complex structures called classes. To use a class, you create<br />

instances of it, known as objects. Classes contain properties <strong>and</strong> methods. You can think of<br />

the properties as the traits of the object <strong>and</strong> the methods as actions you perform on/with<br />

the traits. For example, if you create a class called Cat, some of the properties might be<br />

color, weight, <strong>and</strong> markings, <strong>and</strong> a method might be getBreed(). You use the dot syntax<br />

as a way to connect an object with its properties <strong>and</strong>/or methods. For example, if you<br />

create an object myCat from the Cat class, the object can call the getBreed() method<br />

using dot syntax, like this:<br />

String breedName = myCat.getBreed();

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

Saved successfully!

Ooh no, something went wrong!