AS: Control Structures
Good To Know
AS:Variables by Rantzien
First Few Words
I'm pretty sure some of these I will take up here has been covered already but to easily compare the different control structures I will show 6 different here.
The Control Structures
The different control structures we will examine are:
- if
- for
- while
- do while
- switch
Plus a little extra list on operators.
if statement
The if statement goes by 3 different statements; if, if...else and if...else if.
One example of the if statement is as follows:
if (var1 == 10) {
trace ( "var1 is the same value as 10" );
}
This will check if the variable var1 is more than the value 10.
And if it is var1 is the same value as 10 will be shown in the Output window.
To use the same but with the if...else statement you can use:
if (var1 == 10) {
trace ( "var1 is the same value as 10" );
}
else {
trace ( "var1 is not the same value as 10" );
}
If var1 is not more than 10 the var1 is not the same value as 10 string will be shown in the Output window.
You can use the if, if...else and if...else if statements in the same code like this:
if (var1 == 10) {
trace ( "var1 is the same value as 10" );
}
else if (var1 == 11) {
trace ( "var1 is the same value as 11" );
}
else {
trace ( "var1 is not the same value as 10" );
}
There, now we got the code for the different if statements.
You should pretty easily see how it works since it's very common to the english language.
for statement
The for statement is when you want something to happen until you want it to stop...
Not a very good explanation but I'll show you the built for it:
for ( var i = 0; i < 10; i ++ ) {
}
This is the most common use for it.
First parameter declares a new variable count to the value of 0. If you dont understand this parameter see AS:Variables as posted above.
Second parameter is to see how long it will keep doing this.
On this example it will do the actions as long as i is lower than 10.
So the actions will stop and not go to infinite you have to set the third parameter to do something. In this case it will increase i by 1 until i is not lower than 10.
Example:
for ( var i = 0; i < 10; i ++ ) {
trace ( "You are now " + i + " years old" );
}
This will trace in the Output window:
You are now 0 years old
You are now 1 years old
You are now 2 years old
You are now 3 years old
You are now 4 years old
You are now 5 years old
You are now 6 years old
You are now 7 years old
You are now 8 years old
You are now 9 years old
To stop the loop from looping somewhere you want you can add a break;
for ( var i = 0; i < 10; i ++ ) {
trace ( "You are now " + i + " years old" );
if ( i == 5 ) {
break;
}
}
This will stop the loop from looping when i = 5 so the output will show like this:
You are now 0 years old
You are now 1 years old
You are now 2 years old
You are now 3 years old
You are now 4 years old
You are now 5 years old
See? This was not so hard that you thought right? ;)
while statement
while is very much alike the for statement because it loops until false.
You can also say that it is like the if statement but loops instead of doing it every frame.
I reccomend using for because atleast I think it's more simplier.
Just look at this example between while and for:
while
var i:Number = 0;
while (i < 10) {
trace (i);
i ++;
}
for
for (var i = 0; i < 10; i ++) {
trace (i);
}
So you can use the for statement or while statement and both do the same things.
do while statement
In the while statement the actions will only be trigged while the statement is true.
But in do while the do comes first followed up by the while which makes the actions trigger once even thought it is false;
do{
i ++;
} while (!uber)
switch statement
[ thanks to AS bible for the switch info ]
The switch statement can be said to use many if statements but in another way. This may come in handy in some occasions.
var Value:Number = 10;
switch (Value) {
case 11:
trace ( "11" );
case 10:
trace ( "10" );
case 5:
trace ( "5" );
}
This will be seen in Output window:
10
5
So this basicly checkes everything from its value which in this case is 10 and lower so everything from 10 and lower cases will be shown. Therefore not case 11.
If you want only 10 to be shown you can make a break; as used above:
var Value:Number = 10;
switch (Value) {
case 11:
trace ( "11" );
case 10:
trace ( "10" );
break;
case 5:
trace ( "5" );
}
A Little List of Operators
= - used when decalring a variable: var Value:Number = 10;
== - used when checking a variable: if (Value == 10) { }
> - used when checking if a value is bigger than another value: if (Value > AnotherValue) { }
< used when checking if a value is smaller than another value: if (Value < AnotherValue) { }
Just a small list with common operators.
And that's it for me
-Snail-
