Shorthand conditional statement (if)
Knowing the shorthand notation can save you time and lines of code. I present here two examples of short version of "if" conditional statement.
Example 1:
var age:uint = 22;
//this shorthand if returns true if age > 18 and false if not
var adult:Boolean = (age > 18);
trace(adult); //traces false in this case
Where regular "if" statement would look like:
var age:uint = 22;
var adult:Boolean;
if(age > 18)
adult = true;
else
adult = false;
trace(adult);
Example 2:
var age:uint = 12;
//if condition is true then the first value will be assigned,
// otherwise the latter
var isAdult:String = (age > 18) ? "adult" : "kid";
trace(isAdult); //traces "kid"
Where regular "if" statement would look like:
var age:uint = 12;
var isAdult:String;
if(age > 18)
isAdult = "adult";
else
isAdult = "kid";
trace(isAdult);
Example 3:
var age:uint = 12;
//you can also define a range
var isYouth:Boolean = (age > 10 && age < 20 );
trace(isYouth); //traces "true"
Comments