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

82<br />

switch (numberOfBurritosEaten){<br />

case 0:<br />

orderSuperBurrito(2);<br />

break;<br />

case 1:<br />

orderSuperBurrito(1);<br />

break;<br />

case 2:<br />

orderGr<strong>and</strong>eBurrito(1);<br />

break;<br />

case 3:<br />

orderRegularBurrito(1);<br />

break;<br />

case 4:<br />

orderTacos(2);<br />

break;<br />

case 5:<br />

orderTacos(1);<br />

break;<br />

case 6:<br />

orderDessert();<br />

break;<br />

default:<br />

seekHelp();<br />

break;<br />

}<br />

switch statements, unlike if...else if statements, can’t check a range, but rather check<br />

for a specific value match. The condition inside the parentheses after the word switch<br />

needs to be of type int, char, or byte. The int type is for integers (e.g., –5, 0, or 2300),<br />

the char type is for individual characters on the keyboard (e.g., i, a, or /), <strong>and</strong> the byte<br />

type is a subset of integers—values between 127 <strong>and</strong> –128. I tend to use int values<br />

between the parentheses of my switch statements to keep things simple. In the newest<br />

version of Java (1.5), as of this writing, you can also use what’s called an enumerated type<br />

within the parentheses of switch statements (not currently in <strong>Processing</strong>). However,<br />

enumerated types are an advanced concept that I’m not going to cover here.<br />

When the switch statement runs, the value in the top parentheses is compared to each<br />

case, from top to bottom. If there is a match, the code after the colon is executed until it<br />

comes across a break. When it finds a break statement, the program exits the switch statement<br />

<strong>and</strong> goes to the next line below the entire switch block. The break comm<strong>and</strong> is<br />

optional, but if you don’t use it, the program will fall through <strong>and</strong> execute the code of the<br />

next case, <strong>and</strong> continue checking all the remaining cases until it reaches the bottom of the<br />

statement. In most instances, this would be a waste of processing power. The default<br />

statement (also optional) at the end of the switch executes if no case matches were found.<br />

The last break statement is unnecessary <strong>and</strong> harmless, but possibly helpful if you happen<br />

to add more statements below <strong>and</strong> convert it to a case, so you don’t eventually forget to<br />

add the break statement (a common error). I leave the choice to you.

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

Saved successfully!

Ooh no, something went wrong!