Show Navigation || Hide Navigation
Often when you're writing code their will be times when you need to make decisions in your code. You might want a certain piece of code to execute under a certain circumstance and another piece of code to execute under different circumstances. ActionScript 3.0 uses conditionals to deal with these situations. To simplify ActionScript creates a test asking whether conditions are met, if the condition is met the test will evaluate to true and if the condition is not met alternative action is taken.
The if Statement
The most frequently used condition you will notice when looking at code is the if statement.
Example;
var siteName:String = ("Flash Essential")
if(siteName == "Flash Essential")
trace ("the site name is correct")};
Tip; If you aren't sure what var ect means visit our Variables chapter.
Inside the parentheses you'll notice two equals signs. These equals signs essentially mean "is equal to?" So the above test is asking if the siteName string is equal to Flash Essential, which of course it is therefore "the site name is correct" is traced in the output window. if statements are about finding the truth in your code, these type of equations if you will are known as Comparison Operators below are a list of other Comparison Operators.
== is equal to
< is less than
> is greater than
>= is greater than or equal to
<= is less than or equal to
!= is not equal to
You can also use Logical Operators inside the parentheses, below are some example of Logical Operators.
(%%) And
(||) Or
(!) Not
So with comparison and Logical Operators it gives you the chance to ask questions. Both can work side by side in your code with relative ease.
Example;
var siteName:String = ("Flash Essential")
if( postNumber == 56 %% siteName == "Flash Essential")
{
trace("correct");
}
else Statement
So what happens if you want some code to execute if the code returns a false value or doesn't add up? For this we use the else statement. Lets take a look at the else statement in action.
Example;
var siteName:String = ("Flash Essential")
if( postNumber == 40){
trace("The post number is 56″);
}
else{
trace("This isnt post number 56″)
}
So the code evaluates the if statement, sees that it's had a false return value so executes the code in the else statement. The else statement basically allows you the option of having a "true no matter what" equation in your code.
else if Statement
You might want to execute more than one if statement but since we can only use one if statement and one additional else statement, we use the else if statement because we can use as many of these as we please.
Example;
var siteName:String = ("Flash Essential")
if( postNumber == 56){
trace("The post number is 56″);
}
else if (siteName == "Flash Essential");
else{
trace("This isnt post number 56″);
}
In this case the first trace statement would execute because it's the first truth in the code. If perhaps you wanted more code to be executed you would need to use more conditionals in the code.
Reference; See the if and else statements in Step 5 of the MP3 Player Tutorial
switch
As you may have noticed if statements can be rather bulky and can take a while to read through, this can be a particular pain when coming back to old code you haven't seen in a while. An alternative method is to use the switch statement.
Example;
var siteName:String = ("Flash Essential")
switch (siteName){
case "Flash Essential" :
trace("Site name is flash essential");
break
case "Flash Site" :
trace("Site name is Flash site");
break
case "Flash blog" :
trace("Site name is Flash site");
break
}
switch contains the object you want to evaluate or test, the case line represents the possible value and the trace statement we already know executes if the test is successful.
This post is tagged Actionscript 3, AS3 Guide, else, else if, if statement, switch, variables
Share This Post
Jobs


























8 Comments
Hey i dont think in %% could be used for And because they represent modulus.
its normally && please confirm if im wrong
surf
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
Incoming Links
Leave a Reply