Loops and Conditional statements are used not just in ActionScript but other languages as well making them very important and worth learning how to use.
Conditionals getting startedConditionals include
if else if and
else which are used to look through data given to them and do things if a condition is met.
Conditionals an exampleA basic example of a conditional statement is as follows:
CODE
var i:Number = 10;
if(i<20){
trace("20 is greater than "+i);
}
The above code sets a variable
i as the data type
Number and gives it the value of
10 before pluggins it into an
if statement that checks if
i<20 which translates to english as
if VALUE OF i is LESS THAN 20 DO trace("20 isgreater than "+i);There are other types of conditional statements though, they look like so:
< LESS THAN
> GREATER THAN
<= LESS THAN OR EQUAL
>= GREATER THAN OR EQUAL
|| OR
&& AND
! NOT
!= NOT EQUAL and others that are less commonly used.
With conditional statements you can go through multiple conditions, and the program will to the first one that comes back as true (the condition is correct, or i is LESS THAN 20). That looks like so:
CODE
var i:Number = 10;
if(i<10){
trace("10 is greater than "+i);
}
else if(i<20){
trace("20 is greater than "+i+" which is greater than 10");
}
else
{
trace("i is greater than 20");
}
This code just says
IF VALUE OF i IS LESS THAN 10 TRACE "10 is greater than VALUE OF i" IF NOT, IF VALUE OF i IS LESS THAN 20 TRACE "20 is greater than VALUE OF i which is greater than 10" OTHERWISE TRACE "i is greater than 20"If you wanted to, you could also just stack multiple if statements inline or inside of eachother like so:
Inline Example
CODE
var i:Number = 10;
if(i<10){
trace(i);
}
if(i<20){
trace("20 > "+i);
}
if(i<30){
trace("30 > "+i);
}
Which would trace "20 > 10" and "30 > 10" because both of those statements are true.
Inside of other statements Example
CODE
var i:Number = 10;
if(i<100){
if(i>50){
trace("i is greater than 50");
}
else{
"trace(i);
}
}
Which checks if i is less than 100 and if it is it checks if i is greater than 50, if it is again it traces "i is greater than 50" otherwise (if i is less than 50) it traces "10" (i's value).
Loops going around and aroundLoops, in Flash and other languages, are an integral part of making checks and making sure thateverything that should be done, according to what the loop's code looks like, is done.
Loop BasicsThere are only 2 types of loops in Flash, each of which offer 2 ways of doing the same thing. There is the
while and
for loops.
Looking at the
while loops first we will go through an example of how to use them, what they do and then how to use the
do while loops.
Creating a simple
while loop is as simple as this:
CODE
var i:Number = 1;
while(i<10){
trace(i);
i++;
}
Which will trace out 1 2 3 4 5 6 7 8 9. There is no 10 traced out because i=10 when that would happen, so it can't be done with the current loop.
This is where other loop sttements come into play. Here they are:
< LESS THAN
> GREATER THAN
<= LESS THAN OR EQUAL
>= GREATER THAN OR EQUAL
and so on
As you can tell they are very similar to those used by conditional statements, that is because a loop only works while a certain condition is met, so they both run off of the same thing. All you have to do if you were wanting to also trace out 10 is change the
< to
<=.
Loops do allow for some interesting math when cycling through them (though this is also allowed whenever else you wish to use it).
You can implement the following math functions whenever you wish:
++ PLUS PLUS, OR NUMBER = NUMBER + 1
-- MINUS MINUS, OR NUMBER = NUMBER - 1
+=5 NUMBER = NUMBER + 5
-=5 NUMBER = NUMBER - 5
*=5 NUMBER = NUMBER * 5
/=5 NUMBER = NUMBER / 5
I believe that is all, but I may have missed one, so there are possbily others out there.
Sorry, that got a little off track, but now we are back with a look at the
do while statement.
CODE
var i:Number = 1;
do{
trace(i);
i++;
} while(i<10);
Which outputs 1 2 3 4 5 6 7 8 9 again not 10.
For loops go through a different approach of getting things to work, but work pretty much the same. Here is an example:
CODE
forvar i:Number=1; i<10; i++){
trace(i);
}
This is great for minimizing loop code lengths and allowing you to see everything that is going on for the loop math on a single line.
The for loops have another way of being implemented that makes that a bit more interesting, and somewhat more useful. Lets take a look at them:
CODE
var a:Array = new Array("test", "test2", "test3");
for (item in a){
trace(a[item]);
}
which traces test test2 test3. However you can also add in properties easily when using an
for...in loop, like so:
CODE
var a:Object = {name:'Fred', age:1000, lastName:'Bob'};
for (property in a){
trace(property+" is "+a[property]);
}
Which traces "name is Fred" "age is 1000" "lastName is Bob".
Before we endHopwfully this was helpful and informative on the power you can have in Flash though simple conditional statements and loops.
The End