Variables DSDN 142 Creative Coding
WHAT ARE VARIABLES? Variables are the most important building blocks of programming. Without them we simply cannot achieve anything dynamic or changing, which is the entire point of programming. Essentially they are the computer's memory, they are a way to make the computer remember something you'll need later.
Computers, like people, base any new information from an understanding of what has been in the past. In order for this to be possible past events must be remembered. Variables are the way a computer remembers past information.
The things we can do with variables include: remembering information. Changing the information to remember. Using the information that is remembered.
Variables DSDN 142 Creative Coding
VARIABLES ARE A PO-BOX: Variables can be thought of as a postoffice box, or a pigeon-hole. They have a name attached, and can have things placed inside them for later collection or use. If you need to store a certain number, word or any other piece of information, you simply create a new box and give it a name. To get that information later you simply need to type in the name you originally gave the box, the computer then looks inside and retrieves whatever is currently inside it.
Variables are even better than the standard po-box system, as they can have literally ANY name, not just a number.
Variables DSDN 142 Creative Coding
HOW TO MAKE A VARIABLE: There are only two things you need to create a variable; What type of information it is supposed to hold, and the name of the box.
NAMING RULES: You can name a variable almost anything you want, there are only a couple of rules: a variable name can only start with a letter or an underscore and can only contain alphanumeric characters and underscores (no symbols such as !@#$%^&*() or spaces).
Valid variable names
Invalid variable names
larry important_number my_name33 i_like_bread CircleRadius _variable
an_important_word_&_bread 23_word $money broken variable
Variables DSDN 142 Creative Coding
DATA TYPES: In addition to a name, every variable needs a datatype. In order for the computer to make the correct sized slot for your information, it needs to know what is going to be put into that variable. It is as if you were to talior make a box for a toy car, and tried to fit an apple into it, it simply wouldn't fit. NAME
WHAT INFORMATION IT CAN HOLD
EXAMPLES
int
Numbers without decimal places
1805,
float
Numbers with decimal places
1.22326,
double
HUGE numbers with decimal places
1.2343673765437554,
String
Sentences contaned by quotes
“Harry is already sick of programming tutorials�
char
A single ascii character contained by single quotes
'f'
'%'
23,
55 ,
3.88,
'k'
69385
etc etc.....
437.66 24747.565 etc etc...... 4674643764655373.3764 etc etc.....
'K' '-' '8'
etc etc......
These are the most basic datatypes, but there are many more and you can even make your own!
Variables DSDN 142 Creative Coding
DECLARING A VARIABLE To create a variable you can do one of two things: You can create the box AND put something inside of it now, or you can create the box but leave it empty. To create an empty variable you do the following: datatype variableName; To create a variable and put something inside it: datatype variableName = value; This code creates a storagebox called myNumbe which holds integers but leaves it empty for now.
This code creates a storagebox called myNumberand puts 56 into it ready to retrieve later
Both of these examples create a variable called myNumber, the difference is the first example is left empty whilst the second example has 56 put in it right from the start. The important thing to remember is that you MUST have a datatype and a name.
Variables DSDN 142 Creative Coding
FURTHER EXAMPLES String sentence = “Hello World”; String bruce; float numberWithDecimal = 1.234; int numberWithoutDecimal = 56; What do you think would happen if you tried to put a decimal number or a word into a variable which you said is meant to hold an int? Try it and see what happens. ASSIGNMENT, OR “PUTTING THINGS INTO A VARIABLE” Ok so a variable isn't really any good if it is only ever the number you originally put into it right? The “assignment operator” which is the = symbol, simply means put whatever is on the right side (of the equals) into the variable on the left side. int numberWithoutDecimal = 56; //ok so numberWithout decimal is now 56 numberWithoutDecimal = 102; //56 has been removed and now numberWithoutDecimal is now 102 numberWithoutDecimal = numberWithoutDecimal + 50; // AH HUH sneaky, numberWithoutDecimal is now 152..... why? Notice the datatype (in this case int) was only written ONCE. That was when we created the variable (or the box for a number) in all the proceding lines that box already existed and therefore the computer already knew what type it could hold.
Variables DSDN 142 Creative Coding
USING VARIABLES If you have an int or a float (numbers) variable you can use it ANYWHERE you could put a number. if you have a String variable you can use that variable where ever you need words. Whenever you write out the name of the variable (after it is declared) the computer no longer sees the variable name, but rather looks inside the box that name refers to and sees the value currently residing in it. Code is always excecuted from top to bottom, so as you read these examples read each line in order and notice how the values change as the code goes on.
int a = 43; float b = 23.3; String sentence = “hello world”; println(sentence); //prints the value stored in sentence (minus the quote marks) hello world println(a+b); //this line prints out the value of a+b which is 66.3 line(a, b, a+200, b+200); //draws a line from (43,23.3) to (243, 223.3) a = 645; //replaces the value currently in a (43) with 645. a now contains 645 println(a+b); //notice this is the same code as above, but this time it prints 668.3 b = a – 100; //replaces whatever is in b with a – 100 (645 – 100) which is 545 println(sentence+b); //prints out: hello world545 println(sentence+”b”); //prints out: hello worldb
Variables DSDN 142 Creative Coding
Exercise 1
•
Draw as many black lines, within a white window, size 100x100 pixels with following parameters:
•
line colour black
•
line weight 3 pixels
•
line height 100 pixels
•
10 pixel space between each line
How many lines did you draw? Please only proceed if you figured out the answer.
Variables DSDN 142 Creative Coding
Please only proceed if you figured out the answer
Variables DSDN 142 Creative Coding
Solution for exercise 1 void setup() { size(100,100); strokeWeight(3); background(255); } void draw() { line(0,0,0,100); line(10,0,10,100); line(20,0,20,100); line(30,0,30,100); line(40,0,40,100); line(50,0,50,100); line(60,0,60,100); line(70,0,70,100); line(80,0,80,100); line(90,0,90,100); line(100,0,100,100); }
=
int space=10; void setup() { size(100,100); strokeWeight(3); background(255); } void draw() { line(0*space,0,0*space,100); line(1*space,0,1*space,100); line(2*space,0,2*space,100); line(3*space,0,3*space,100); line(4*space,0,4*space,100); line(5*space,0,5*space,100); line(6*space,0,6*space,100); line(7*space,0,7*space,100); line(8*space,0,8*space,100); line(9*space,0,9*space,100); line(10*space,0,10*space,100); }
There are 11 total lines within the window. The piece of code to the right uses a variable called “space� to ensure that the lines are spaced out at even intervals, in this case 10 pixels.
Variables DSDN 142 Creative Coding
Exercise 2
Change the code on the right so that the lines are spaced out by 12 pixels from each other. How many lines now fit in your window?
int space=10; void setup() { size(100,100); strokeWeight(3); background(255); } void draw() { line(0*space,0,0*space,100); line(1*space,0,1*space,100); line(2*space,0,2*space,100); line(3*space,0,3*space,100); line(4*space,0,4*space,100); line(5*space,0,5*space,100); line(6*space,0,6*space,100); line(7*space,0,7*space,100); line(8*space,0,8*space,100); line(9*space,0,9*space,100); line(10*space,0,10*space,100); }
Variables DSDN 142 Creative Coding
Please only proceed if you figured out the answer
Variables DSDN 142 Creative Coding
Solution 2 int space=12; void setup() { size(100,100); strokeWeight(3); background(255); } void draw() { line(0*space,0,0*space,100); line(1*space,0,1*space,100); line(2*space,0,2*space,100); line(3*space,0,3*space,100); line(4*space,0,4*space,100); line(5*space,0,5*space,100); line(6*space,0,6*space,100); line(7*space,0,7*space,100); line(8*space,0,8*space,100); line(9*space,0,9*space,100); }
There are nine lines in the window. This demonstrates how variables can make our sketches more flexible and dynamic. Note: Only the variable “space� was changed. Also, the equals sign in Processing works in a different way than the equals sign in maths.