openobject.org

Using Variables in Processing

From Physical Programming

Contents

Variable Types

Variables are used to store data, you can think of them as containers. Different types of variables are used for different types of data. When using variables in Processing you need to specify the variable datatype. This need only be done once when you create the variable. Some common variable types are documented below.

Type: Integers
Code: int
Description: A whole number (a number without a decimal point).
Use:

int a  =  5;
line(0, 0, a, 10);

Type: Floats
Code: float
Description: A floating-point number (a number that may have a decimal point).
Use:

float a  =  2.597;
line(0, 0, 200/a, 100);

Type: Boolean
Code: boolean
Description: Boolean variables have only two possible states, they can be either true or false. Booleans are commonly used in control statements (like if structures).
Use:

boolean x  =  false;
if (x) {
	line(0, 0, 100, 100);
} else {
	line(100, 0, 0, 100);
}

Type: Characters
Code: char
Description: Characters are typographical units such as letters of the alphabet and special symbols (i.e. A, a, d, %). When defining a character you must place it between single quotes.
Use:

char something  =  'H';
if (something == 'H') {
	text(something, 100, 100);
}

Type: Strings
Code: String
Description: A String is a sequence of characters. The String datatype is capitalized because it is actually a class of object. When you create a String you are creating an object and therefore you can use the class methods to access various properties of that object (some of these are shown below). When defining a String you must surround the group of characters with double quotes.
Use:

String something  =  "what I want to say is";
if (something.length() > 15) {
	text(something.toUpperCase(), 100, 100);
}

Type: Color
Code: color
Description: Color is a datatype for storing color values. Colors may be assigned with the color() function or by using hexadecimal notation.
Use:

color c1 = color(255, 0, 0);
color c2 = #FFCC00;
fill(c1);
rect(0, 0, 25, 100);
fill(c2);
rect(25, 0, 50, 100);


Variable Useage

Variables can be used in the same manor as their equivalent values.

int i = 5;
point(i, i);

Produces the same result as:

point(5, 5);

Variables can also take their value from other variables.

int x = 5;
int y = x + 10;
point(x, y);

And when assigning a value they can also reference themselves.

int i = 5;
point(i, i);
i = i + 10;
point(i, i);

Processing provides special operators for manipulating variables in this way.

i += 3; 	// this command adds 3 to the variable i
i -= 3; 	// this command subtracts 3 from the variable i
i /= 3; 	// this command divides the variable i by 3
i *= 3; 	// this command multiplies the variable i by 3

In situations where you are incrementing or decrementing the value by 1 then the following shortcuts may also be used.

i++; 	// adds 1 to the variable i
i--; 	// subtracts 1 from the variable i

The 'increment by 1' operator is commonly found in for loops.

for (int i = 0; i < 40; i++) {
	line(30, i, 80, i);
}


Variable Conversion

Processing provides functions for converting between datatypes. Some of these functions are listed below.

Code:

char()
int()
str()
boolean()
float()

Use:

char c; 
float f;
int i;

c = 'A';
f = float(c);       // Sets f = 65.0
i = int(f * 1.4);  // Sets i to 91

rect(f, 0, 40, 66);
fill(204);
rect(i, 67, 40, 66);

Variable Scope

Variables may be either local or global. If a variable is defined inside a function or class (such as within the setup() or draw() functions) then it is only available for use within that function or class. If a variable is defined outside a function or class then it becomes a global variable and can be used anywhere within the program.