Just a quick mini-tutorial on using the ternary operator in ActionScript. One of those things that shows up in my search logs, so the next time someone searches for it, they should be able to find it.
The ActionScript Ternary Operator
The ternary operator, "? :", is basically a shorcut for writing quick conditional statements that evaluate to either true or false. Similar to an if/else statement, you use them to make a decision in your code, and act based on that decision.
Here's the syntax:
-
// using the ternary operator
-
(condition) ? isTrueAction : isFalseAction;
-
The ( ) around condition are optional, however they do help to clearly illustrate the condition. The code above is the same as writing:
-
// the same thing, only more typing
-
if (condition) {
-
isTrueAction;
-
} else {
-
isFalseAction;
-
}
-
Now, you won't be able to use this for something that has multiple statements after the condition like you can in an if statement, but it does have it's uses for quick little things. For example, you couldn't use it for something like this:
-
// this can't be written out using ternary operators
-
if (condition) {
-
isTrueAction;
-
var bob:String = "something";
-
goDoSomethingNow();
-
} else {
-
isFalseAction;
-
}
-
But it's great for something like this:
-
// this is a great use for ternary operators
-
(superBowlWon == true) ? gotoDisneyland() : goHome();
-
Or even for checking if a number is even or not.
You can also use this technique to set values for variables:
-
// setting a variable like this...
-
var pillToTake:String = (stayInMatrix == true) ? "blue" : "red";
-
//
-
// is the same as doing this:
-
if (stayInMatrix == true) {
-
var pillToTake:String = "blue";
-
} else {
-
var pillToTake:String = "red";
-
}
-
So, as you can see, using this operator can save you time by requiring less typing in a few situations. Now, it's not the solution to everything, but as you start using it more, you'll start to wonder how you worked without it. (Maybe not, but it'll make me feel better if I can imagine that I've somehow improved your life.)
Also, after I finished writing this, I decided I wanted to try and determine which method is faster, to see if there's another argument for learning how to use this stuff. So in a pretty informal, and un-scientific way, I ran the following test:
-
// testing ternary operator vs. if/else
-
// just copy and paste onto the main timeline, and run
-
//
-
var loops:Number = 10000;
-
//
-
// ternary test
-
var startTime1:Number = getTimer();
-
for (var i:Number = 0; i < loops; i++) {
-
var testNum:Number = Math.floor(Math.random() * 100);
-
var isEven:Boolean = ((testNum % 2) == 0) ? true : false;
-
}
-
var endTime1:Number = getTimer();
-
var totalTime1:Number = endTime1 - startTime1;
-
//
-
// if/else test
-
var startTime2:Number = getTimer();
-
for (var i:Number = 0; i < loops; i++) {
-
var testNum:Number = Math.floor(Math.random() * 100);
-
if ((testNum % 2) == 0) {
-
var isEven:Boolean = true;
-
} else {
-
var isEven:Boolean = false;
-
}
-
}
-
var endTime2:Number = getTimer();
-
var totalTime2:Number = endTime2 - startTime2;
-
//
-
trace("Total Times: \n");
-
trace("\tTernary: " + totalTime1);
-
trace("\tif/else: " + totalTime2);
-
Running this test on my PowerMac with dual G5 2.0, and 1 GB ram, I came up with the following averages after running it 20-30 times: Ternary: 201 ms, if/else: 195 ms. Now, your results may vary, and this most likely isn't the best way to test something like this, but it's enough for me to see that there is a speed difference, but it's not enough of a difference to really make me stick to if/else exclusively.