VariablesRead 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:
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 int, float 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:
Math with VariablesStart 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:
Common Mathematical Operators:
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;
6=Y*X;
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:
X=6/Y
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 LoopComputers 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:
New Code:
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:
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:
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. |
Intro to Processing >