Variables and Math


Read Pages 37 - 40 of Getting Started with Processing about Variables and watch the video below. Often we'll want to use the same number over and over again. For example, lets say you want to fill the screen with squares that are 20x20. So, you write the code for 30 or so squares and run the sketch, only to find 20x20 is too small and you want 30x30. Now you have to go in and change every one. If you'd used a variable, you could change one number and re-run the sketch in moments.

Variable Types:
 Type   Range  Notes
 int  +- 2,147,483,647  Whole Numbers only 
 float  +-3.40282347E+38   Decimals 
 long  +-9,223,372,036,854,775,808   Whole Numbers only

You should refer to the Processing Reference for more information on data types. You will use variables of varying data types throughout your programs. There are other types of data Processing programs can use. We'll introduce more as we need them.

Note: Using intfloat or, long declares a variable. You only do this once for each variable. Afterwards you can reassign a variable simply using an equals sign. 

For Example:
int a = 10;  //Declares the variable and sets it equal to 10

a = 30;      //Replaces the 10 with 30

Math with Variables

Start off by watching the video below and reading pages 40-42 in Getting Started with Processing. The important idea here is that you can use mathematical operators with numbers and variables in Processing. This is incredibly useful and it will be something you'll be doing a lot of.

Factors to remember:
  • Order of operations matters. 3+2*3 will yield 9. If you want it to yield 15 then you must write it as (3+2)*3.
  • You can not use x as a symbol for multiplication. x is a letter and will be viewed by Processing as a variable.
  • An equals sign works a bit differently than it does in math class.
Common Mathematical Operators:
 +  Addition
 -  Subtraction
 * Multiplication 
 /  Division
 ^  Exponent

Meaning of the Equals Sign:
The equals sign is used to assign a value to a variable. Processing will not rearrange an equation to solve for a value. So:

int Y=3;

Processing will not be able to determine that X is 2. If you want to assign the value of 2 to X based on the relationship above you must write it as:


Mathematically 6=Y*X and X=6/Y are equivalent. However, Processing is only able to understand the second expression. In plain english, X=6/Y means, "Assign the value of 6/Y to the variable X".

For Loop

Computers are really good at repetition. So farif you want to do something over and over again you've had to do all the thinking and the copying and pasting of code. Pages 42-48 of Getting Started with Processing introduce a really cool idea. Watch the video, try the code below and then work through all the examples in the book.

Try this code:
size (100,100);
smooth ();

New Code:
size (100,100);
smooth ();
for (int i=0; i<width; i+=10)

The "for" process above will draw lines that are in successively different positions. You'll notice " int i=0" in the for statement. In this case i only has meaning inside the for loop. This relates to variable and scope. We'll cover scope in the next unit. Play with this code and make some cool pictures.

The basic structure of "for" is as follows:
 Initialize the Process
 Set the Condition
 int i=0
 i < width

Try to elaborate on my program by drawing more than one line in the for loop. Create an image inspired by the one to the right. Be sure to spruce it up a bit. Black and grey are pretty boring, add some color as well.

Comparison Operators:
You are not limited to greater than or less than in your conditions within a for loop. Below is a list of comparison operators that can be used:

 x==y  x is equal to y
 x!=y  x is not equal to y
 x>y  x is greater than y
 x>=y  x is greater than or equal to y
 x<y  x is less than y
 x<=y  x is less than or equal to y

The operator that is most important to mention is "x is equal to y". The double equals sign is not a typo. A double equal sign is used to ask the question, does x equal y. If I used a single equals sign "x=y" I would be saying set the value of x to be the value of y. This difference is very important.