Sunday 7 December 2008

Variables First Draft

In programming everything has to have a unique identifier, the processor will remember the values stored for each variable but will we?
Giving variables significant names really cuts down on maintenance and the amount of commenting needed. The first character in a name should usually be a letter. Numbers and symbols wont work. An underscore can be used but this should be used to denote special cases. As long as the first character is a letter though, we will not go wrong. Its traditional for normal variables to start with a lowercase letter and to capitalise the first letter of each subsequent word making up the name. e.g.
account
goldAccount
superDuperAccount
This is known as camelCase format
Variable values are usually 'variable' and are changed within the program, but occasionally variables whose values dont change are needed. These are called constants because their value is constant and never changes. Constants are usually uppercase e.g.
MAXROWS
MINHEIGHT
NUMMONSTERS
Later on we will be using a structure known as a class, classes are used to define object types and they should always start with an uppercase letter. e.g:
Shape
Room
Person
We use built in classes to specify each variable's type and the most used types are String, int, float and boolean: e.g.:
String name;
int width;
float amount;
boolean isVisible;
Variable types need to be declared before the program starts. Later their values need to be declared e.g.:
name="circle";
width=800;
They can also be declared and initialised at the same time:
int float=0.01;
boolean isVisible=false;
Giving a variable a value is known as assignment, and always uses the = operator.
You must be very careful if you are comparing values to make sure that you use == and not = as they have very different consequences!
a=6+4; (result 10)
a==6+4 (result true or false-depending on the value of a, if a = 10 then true)
When comparing strings it is usual to use the equals() method for comparison. This is because == will only be true if the strings compared belong to the same object. If we have two strings a and b and they both have the same value then they will not be equal when using ==.
a="value";
b="value";
a==b result is false
a.equals(b) result is true
The scope of a variable affects where it is recognised.
Global variables are declared at the start of the program and can therefore be used in any code block. The variables in lines 11-18 are global.
Variables declared within a code block are known as local variables and are only usable within that block. The local variables declared on lines 33 and 34 are usable at any point within the draw function. None of the other functions are aware of their existence.
Local variables, declared within different blocks, may have the same identifier, but they will refer to different objects.
The integer n in the drawCircles(int) function is declared within a for loop. Line 110. If we were to try using it anywhere outside the for block, it would not be recognised. If we wanted to access its value in the drawCircles(int) function we would need to declare it outside the for block. eg:

void drawCircles(int gap){
int n;
for(n=0;n< round(random(width/2,width));n+=gap){
drawCircle(n); }
println("Final value of n: "+n);
}

No comments:

Post a Comment