QuickTip: Inline If-Then-Else Statement

This entry is part of a series, Quick Tips»

If you have to do a simple If-Then-Else Statement, it can be tedious to do it this way:

var answer:String;
 
if ( isCreative)
{
	answer = "Aww.. I don't know what to do!";
}
else
{
	answer = "Ha, I know just what I'm going to do!";
}

Here is the easy way:

var answer:String = isCreative ? "Aww.. I don't know what to do!" : "Ha, I know just what I'm going to do!";

This is only really useful for simple things, so if you’re checking lots of conditions or performing lots of operations inside the curly brackets, it might be not be a good idea to use this!

Posted on January 24th, 2011
Filed under ActionScript 3, Flash, Tutorial |

Leave a Reply