The Conditional Operator

     

The conditional operator is a slightly optimized way of implementing if-else logic. It's like an if that requires an else , in contrast to the normal if statement, for which the else is optional.

The conditional operator always specifies both a true and a false condition, making it a ternary operator, meaning it has three arguments. Because it is the only operator that always has three arguments, it is sometimes also called the ternary operator. Its format is

  condition  ?  result_if_true  :  result_if_false  ; 

The first part of the conditional expression ( condition ) is a test that returns true or false . The next two parts are possible return values. If the condition is true , the operator returns part two. If the condition is false , the operator returns part three. In pseudocode, here's a condition that says, "If the journey is greater than 1,000 miles, then fly; if it is not greater than 1,000 miles, then drive."

 journey is greater than 1000 miles ? fly : drive 

Here's how that example might look in ActionScript:

 distance = 2000; howtoGo = distance > 1000 ? "fly" : "drive"; trace(howToGo); // fly 

The third line is the equivalent of

 if (distance > 1000) {      howtoGo =  "fly" } else {      howtoGo = "drive" } 

You can use functions in the second and third parts, too, thus using the conditional operator to control program flow. For instance:

 function fly() {      trace("fly"); } function drive() {      trace("drive"); } miles = 2000; miles > 1000 ? fly() : drive(); // displays "fly" 

You can check multiple conditions with nested conditional operators. For instance, suppose you want to fly if the distance is more than 1,000 miles, walk if it's 3 miles or less, and drive otherwise . In that case, you would add a walk() function to the previous program:

 function walk() {      trace("walk"); } 

Then you can do all this in a single line of code:

 Ask "Is the distance greater than 1000 miles?" If yes, fly. If no, ask, "Is the distance greater than 3 miles?" If yes, drive. In no, walk. 

In the following example, the distance is not greater than 3 miles, so the walk() function is executed:

 miles = 3; miles > 1000 ? fly() : miles > 3 ? drive() : walk(); // displays "walk" 

In this case, the conditional operator is faster and produces a smaller SWF than if/else , but not by much.

In the following instance, if-else would be slightly lighter and faster, but the conditional operator is more readable:

 (day == "Monday") ? trace("is fair of face") : (day == "Tuesday") ? trace("is full of grace") : (day == "Wednesday") ? trace("is full of woe") : (day == "Thursday") ?  trace("has far to go") : (day == "Friday") ? trace("is loving and giving") : (day == "Saturday") ? trace("works hard for a living"): (day == "Sunday") ? trace("is bonny and blithe and good and gay.") : null; 

Notice the use of null as the final option. The conditional operator must always return something, so the choice is between using something like null or choosing one of the trace actions as a default. For instance, to make the final trace the default, change the last line to look like this:

 trace("is bonny and blithe and good and gay."); 

Using the final trace as a default implies that the variable day will always have a valid day of the week as a value. Thus, if it isn't "Monday" through "Saturday" , it must be "Sunday" .



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net