iD Tech Camps

Save money on camp with this special offer

Search Our Site

The World's #1 Tech Camp

iD News and Blog

Request a Brochure

The Ternary Operator in Flash (if-else shorthand)

Most people fluent in Flash Actionscript are familiar with unary and binary operators, but there is also a ternary operator that can be quite useful. First, a refresh on operators:

Unary: count++;

Binary: count = count * 2;

Ternary: var result:String = (count == 0) ? “Empty” : “Not Empty”);

So what does this mean? You can use the ternary form of the if statement as a shorthand. Let’s look at the equivalent full if-else statement:

var result:String;
if (count == 0) result = “Empty”;
else result = “Not Empty”;

Of course, using the ternary operator cuts down on code readability, but three lines can effectively be cut down to one. Remember, the form is this:

(condition) ? ifTrueDoThis : ifFalseDoThis;

February 9th, 2009 | Tags: , , , , , ,

Posted in: iD Tech Bloggers

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This entry was posted on Monday, Feb 09th at 11:33 am and is filed under iD Tech Bloggers. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Blog Categories

ACCED-I Meeting Exelence On CampusThe World's Best Summer Camps

Blog Archives

CEO's Blog

Tech Bloggers