Making Decisions with Conditionals and
switch
During the course of the day, you make countless decisions. Everything you do depends on decisions large and small. Just
eating
a meal involves decisions, from what to eat to whether to use a fork or spoon.
With code, complex behaviors can be broken down into a series of simple decisions. These are called
conditionals
, or tests. A test is run, and depending on the result, an action is taken. For example, you may want to check whether the
user
entered the correct password before running more of the program.
Before jumping into conditionals, I want to
briefly
mention operators. Operators are the "glue" of ActionScripting. They are symbols that tell the code to do something specific. For instance, when you work with conditionals you'll often see the ==, &&, and operators. The == operator
compares
two elements of a statement, and requires that the elements be equivalent before another section of code can run. The && operator requires multiple conditional statements to be true. The operator requires one or another conditional statement to be true. This is why it is called the
or
operator.
There are two main formats for independent conditionals in Flash: 1)
if, if-else
, and
if-else-if
, and 2)
switch
.
Using the
if
Statement
When the Flash compiler encounters an
if
statement, it looks at the condition within the parentheses. If the condition is true, the code within the curly braces is executed. A general
if
statement looks like this:
if(condition){
//do something
}
A simple condition compares two expressions, and returns true if the condition is met. Several types of comparisons can be made between expressions. For example, you may want to know whether two expressions are equal or different. Or you may want to know whether one expression is greater than another.
Let's say that you have a project where you have a movie clip instance for the sky in the background of an animation. What if you want to change the
color
of that instance to blue, when timeOfDay is
"dawn"
? Before changing the color of the movie clip instance, you first need to determine what color to change it to. In ActionScript, you might write the following, which compares the value of
timeOfDay
to
"dawn"
. If they are equal, the code between the curly braces is run and the variable
skyColor
is assigned the value of
"blue"
.
if(timeOfDay == "dawn"){
skyColor:String = "blue";
}
Although this will work, you might want to change the color at various times of the day. An
else
after the first closing curly
bracket
, followed by a second set of curly brackets enclosing another line of code,
presents
an alternative code to execute if the condition is not true. In this case, if
timeOfDay
is
"dawn", skyColor
is
"blue"
. If not,
skyColor
is
"black"
. This is called an
if-else
conditional.
if(timeOfDay == "dawn"){
skyColor:String = "blue";
} else {
skyColor:String = "black";
}
You can set another conditional after an
else
. In fact, you can write a whole sequence of
if-else-if
statements. In the following example, the variable
daytime
has a range of numeric values. Notice that the conditional statements are more complex. By using the && logical operator, you can test for two or more conditions at once. Here, it tests whether the value is between specific number ranges:
if(daytime > 0500 && daytime 1130)
skyColor:String = "blue";
} else if (daytime >1130 && daytime <= 1230)
sky Color:String = "bright_blue;
} else if (daytime > 1230 && daytime <= 1900){
skyColor:String = "blue";
} else {
sky Color:String =" black";
}
Using the
switch
Statement
The
switch
style of conditionals can be used when you have a list of specific values that you are testing for equality. It works just like an
if-else
statement, but the code is formatted differently. First, you state that this is a
switch
conditional, with the variable to be
tested
in parentheses. Then you list each case that you want to test. The syntax for each case is as
follows
. Note the
colon
after the case value, which serves as shorthand for the curly braces.
case "value":
//code to execute goes here;
The following example shows a series of cases to compare values for the variable
daytime
. Remember that the keyword
case
tells
the compiler to test whether the
next
expression is equal to the variable in parentheses.
switch(daytime){
case "dawn":
skyColor:String = "dark_blue";
break;
case "morning":
skyColor:String = "blue";
break;
case "noon":
skyColor:String = "light_blue";
break;
case "afternoon":
skyColor:String = "blue";
break;
case "evening":
skyColor:String = "dark_blue";
break;
case "night":
skyColor:String = "black";
}
Switch
statements are sometimes easier to read, and are better suited to specific cases than to a range of possible values.
|