AS3 If statement syntax shortcut

November 11, 2008 by jared 

var v : Boolean = true;
var i : int;
v ? i = 0 : i = 1;

Is the same as

var v : Boolean = true;
var i : int;
if (v)
     i = 0;
else
     i = 1;

Comments

7 Responses to “AS3 If statement syntax shortcut”

  1. Dwight on November 12th, 2008 1:14 am

    You wouldn’t believe this! Just yesterday I was looking for this :-P

  2. Philippe on April 14th, 2009 9:56 am

    and you can do this too:

    i = v ? 0: 1;

  3. sinclairc5 on May 12th, 2009 7:01 pm

    also try this:

    v ? [ trace( "this" ), trace( " is true" ) ] : [ trace( "DOH!" ) ] ;

  4. sinclairc5 on May 12th, 2009 7:19 pm

    or how about:

    var v = false;

    var a = [ c, d ];
    var b = [ d, c ];

    v ? a.forEach(e) : b.forEach(e);

    function c():void{ trace( “c” ) }
    function d():void{ trace( “d” ) }
    function e(…args):void{ args[0]() }

  5. sinclairc5 on May 12th, 2009 7:23 pm

    some more random stuff:

    var v = “testing”;

    v is String ? [ c(), d()] : [ d(), c() ];

    function c():void{ trace( “c” ) }
    function d():void{ trace( “d” ) }

  6. jared on May 12th, 2009 10:19 pm

    very nice.

  7. mhaine on August 5th, 2009 8:57 pm

    if statement

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!