So I didn't find a tutorial in the AS: Main list about this, so I thought I'd make one.
So, first I'll post an example of two ways you can use the conditinal operator:
Example 1
var a:Number = 5;
a<10 ? trace("a is less than 10") : trace("a is more than 10");
// Output : "a is less than 10"
Example 2
var b:Number = 20;
var c:String = b<10 ? "b is less than 10" : "b is more than 10";
trace(c);
// Output : "b is more than 10"
Now, to begin with the first one, I'll explain it closer. This example might make it more understandable:
condition ? if the condition is true, do this : else do this;
This code is only a shortcut for this:
if(condition){
// if the condition is true, do this
} else {
// else do this
}
In other words, you dont need to know this shorthand, but it sure can be nice to know. You can also nest statements inside each other, like this:
var a:Number = 15;
a<10 ? trace("a is less than 10") : a<20 ? trace("a is between 10 and 20") : trace("a is higher than 20");
This is the same as writing this:
var a:Number = 15;
if(a<10){
trace("a is less than 10");
} else if(a<20){
trace("a is between 10 and 20");
} else {
trace("a is higher than 20");
}
If you don't want any else at the end, simply write:
condition ? if the condition is true, do this : 0;
For the second, it will simply return the value, like this:
condition ? if the condition is true, return this : else return this;
If you have any questions, feel free to ask.