06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.2<br />

Variable Declaration and Initialization<br />

Variables 47<br />

Exercise 4-1: Consider the game Pong. What variables would you need <strong>to</strong> program the<br />

game? (If you are not familiar with Pong, see http://en.wikipedia.org/wiki/Pong ).<br />

Variables can hold primitive values or references <strong>to</strong> objects and arrays . For now, we are just going <strong>to</strong> worry<br />

about primitives—we will get <strong>to</strong> objects and arrays in a later chapter. Primitive values are the building<br />

blocks of data on the computer and typically involve a singular piece of information, like a number or<br />

character.<br />

Variables are declared by fi rst stating the type, followed by the name. Variable names must be one word<br />

(no spaces) and must start with a letter (they can include numbers, but cannot start with a number). Th ey<br />

cannot include any punctuation or special characters, with the exception of the underscore: “ _ ” .<br />

A type is the kind of data s<strong>to</strong>red in that variable. Th is could be a whole number, a decimal number, or a<br />

character. Here are data types you will commonly use:<br />

• Whole numbers, such as 0, 1, 2, 3, � 1, � 2, and so on are s<strong>to</strong>red as “ integers ” and the type keyword<br />

for integer is “ int ” .<br />

• Decimal numbers, such as 3.14159, 2.5, and –9.95 are typically s<strong>to</strong>red as “ floating point values ” and<br />

the type keyword for floating point is “ float ” .<br />

• Characters, such as the letters ‘ a ’ , ‘ b ’ , ‘ c ’ , and so on are s<strong>to</strong>red in variables of type “ char ” and are<br />

declared as a letter enclosed in single quotes, that is, ‘ a ’ . Characters are useful when determining<br />

what letter on the keyboard has been pressed, and for other uses involving Strings of text (see<br />

Chapter 17) .<br />

int count<br />

type<br />

fi g. 4.4<br />

In Figure 4.4 , we have a variable named “ count ” of type “ int, ” which stands for integer. Other possible<br />

data types are listed below.<br />

name<br />

;

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

Saved successfully!

Ooh no, something went wrong!