If & Else
So, the director shouts, “Action!” and both the car and the actor start their courses. Then the director yells, “Cut!” The actor was supposed to wait until the car went by and then walk, or wait at least 5 seconds. And if the car hadn’t passed in five seconds the actor was supposed to go back to his marker and prepare to redo the shot.
An Actionscript may say it like this:
EXAMPLE 1
var actor1=1
var actor2=2
var actor3=3
if (actor3==2){trace(2);}
//This will trace nothing.
EXAMPLE 2
var nabooleanDynomite: String= “Eat the food” ;
var llama: String= “aaaagghh”;
if (nabooleanDynomite == Eat the food){
trace (llama);}
// This traces aaaagghh
We have said that if “nabooleanDynomite” says “Eat the food” then trace the llama’s script which is “aaaagghh”.
The “==” means “is”. It does not mean equals. One equal sign is used to set a value. Two equal signs are used to evaluate a value (to check it).
Else
In the first example nothing was traced because “actor3” was not 2. If we wanted to do something else if the other thing doesn’t check out, we use the else statement.
var actor1=1
var actor2=2
var actor3=3
if (actor3==2){trace(2);}
else {trace(“not 2”)};
“else” can also be read as “otherwise”. So, the simple version of if and else is:
if (something){do something;}
otherwise {do something else};
Notice that the if and else statements are set up like functions. The stuff they do has to be written inside of the braces.
If both things
if (actor1 == 1 && actor2 ==2) {
trace("actor1 and actor2");
}
The “&&” operator checks to see about another thing as well. This is read:
//if actor1 is one and actor2 is 2 trace “options a and b”
// Traces: options a and b
If one or the other
if (actor1 == 9078 || actor3 == 3) {
trace("actor1 or actor3");
}
The “||” operator checks to see if “either or” check out. This is read:
//if actor1 is 9,078 or actor3 is 3, trace “actor 1 or actor3”
//Traces: actor1 or actor3
We know “actor1” is not 9,078, but “actor3” is 3 and so it traces.
If not(!)
if (!actor3)
{trace("not actor3");
}
The “!” operator checks if something “is not” something. This is read:
//if not actor3, trace “not actor3”.
If not equal(!=)
if (actor1 != 1) {
trace("actor1 does not equal 1");
}
“!” next to a single “=” means “not equal”. Read:
//if actor1 is not equal to 1, trace “actor1 does not equal 1.”
Else(Otherwise)
if (actor1 != 1) {
trace("actor1 does not equal 1");
}
else {trace("actor1 does equal 1");
}
//Traces: actor1 does equal 1
Else if
if (actor1 == 2) {trace("actor1 does not equal 1");
}
else if (actor1 == 1) {
trace("actor does equal 1");
}
“else if” can be read as, “otherwise if…” The above is read:
//if actor1 is 2 trace “actor1 does not equal 1”. Otherwise, if actor1 does equal 1, trace “actor1 does equal 1” .
// Traces: actor1 does equal 1
Otherwise if…. if anything else…
if (actor1 == 1) {
trace("it is actor1");
}
else if (actor2 == 2) {
trace("it is actor2");
}
else {
trace("it is something else");
}
Here is a note on this from Oreilly
--------------------------------
The if statement requires one if, only one optional else can be used, and
any number of optional additional else if tests can be used. In all cases,
however, only one result can come from the structure. Consider the above
example, in which all three results could potentially execute—the first two
because they are true, and the last because it is an unconditional alternative.
-----------------------------------------
In the case above, actor1 is 1 and actor2 is 2. Since actor1 is true, only “it is actor 1” will trace. The above is read:
//If actor 1 is 1 trace “it is actor1”. Otherwise if actor2 //is 2 trace “it is actor2”. If anything else trace “it is //something else”.
From Oreilly (on the above script)
--------------------
In this case, only “actor1” would appear in the Output panel because the
first truth would exit the if structure. If you needed more than one execution
to occur, you would need to use two or more conditionals.
------------------------
if (actor1 == 1) {trace("its actor1");
}
if (actor2 == 2) {trace("its actor 2");
}
else {trace("someone else");
}
//Traces: “its actor1” and “its actor2”
From Oreilly (on the above script)
---------------------------
The above structure, for example, executes the first trace in each if, by design.
-----------------------------
Read:
//If actor1 is 1 trace its “actor1” , if actor2 is 2 trace “its actor 2” , otherwise trace “someone else”
Don’t forget that though we are simply tracing things, these statements can be used to start or stop functions and variables. These statements are like stage directions or place markers. They can also be used to adjust properties also.
Switch
This, “if else, otherwise if, otherwise…” stuff can go on forever. It can start to look messy an confusing, unless you are able to read them naturally. However, for people who like shortcuts, there is a way.
Resources for some information listed can be
found in the Resource.pdf file.