Conditional Switch Statements in AS3

This is something I wasn’t aware that you could do in a switch/case statement until recently.  A lot of times developers will handle this with nested if/else statements.

For Example:

var myNum:Number = 7;

switch (true)
{
  case (myNum < 5):
  trace ("< 5")
break;

case (myNum > 5):
  trace ("> 5")
  break;
}

Using switch (true)/switch (false) is pretty cool. You can use it for any sort of true/false comparisons, including comparing strings. The first one to evaluate true will trip the statement.

  1. Hey, that’s pretty cool! It’s just too bad switch is so slow: http://jacksondunstan.com/articles/793

  2. True it is really slow so I would not use it in anything too performance intensive. I have made a number of small apps or widgets in the past where this would have been handy… :$ Thanks for the link!

  1. No trackbacks yet.

 


Warning: require_once(/home/jeffguthrie/jeffguthrie.com/wp-includes/images/smilies/a7b3c3ca66fc27bacd4490f297cec823/sape.php) [function.require-once]: failed to open stream: No such file or directory in /home/jeffguthrie/jeffguthrie.com/wp-content/themes/monochrome/footer.php on line 31

Fatal error: require_once() [function.require]: Failed opening required '/home/jeffguthrie/jeffguthrie.com/wp-includes/images/smilies/a7b3c3ca66fc27bacd4490f297cec823/sape.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/jeffguthrie/jeffguthrie.com/wp-content/themes/monochrome/footer.php on line 31