AS3 Basics: Condensing if Statements (aka shorthand)

Lately I’ve been finding that the way in which i write code hasn’t really advanced at all since i started 3 years ago. Sure I’ve learnt more of the Adobe AS3/Flex library/s, and learnt several new workflows but the actual way which i write code hasn’t changed. So in this tutorial I’ll teach you how to write condensed if statements. and some other methods of doing comparisons.

For instance i might write something like this quite often:

if(myCheckBox.selected == true)
{
    someVar = true;
}
else
{
    somevar = false;
}

This is probably the most long winded way of performing that simple action.

Heres one way the if statement can be condensed by using shorthand:

if(myCheckBox.selected == true)
    someVar = true;
else
    somevar = false;

See much cleaner :)

You can omit the braces {} if you only have one statement in the if body. For instance this will not work:

if(myCheckBox.selected == true)
    someVar = true;
    someFunction();
else
    somevar = false;

This would result in a compiler error.

However for this particular operation (changing a value to true or false with a comparison) we can make it even more simple:

someVar = (myCheckBox.selected == true);

This works because the comparison evaluates to true or false.

But say you want to use something other than true or false, how would you do that? The answer is quite elegant:

someStringVar = (myCheckBox.selected == true)?"Checked":"Not Checked";

Here is the syntax if it’s not completley clear (from adobe livedocs):

expression1 ? expression2 : expression3

Evaluates expression1, and if the value of expression1 is true, the result is the value of expression2; otherwise the result is the value of expression3.

Well i hope this little snippet will help you write cleaner more efficient code :)

Peace

Bill

Subscribe

Subscribe to our e-mail newsletter to receive updates.

  • Lderan

    Ah the ternary operator, always good fun. They have been more of readability thing for me in the past. I can see how they are more efficient for AS3 and more then likely php.

    Another thing with if statements with booleans in other languages you don’t need the == true part. for example.

    if(bobIsAlive)
    System.out.println(“bob is alive!”);
    else
    System.out.println(“poor bob :( “);

    Is this the same with AS3?

  • http://www.marketmetweet.com Tammy

    So… is As3 still “actionscript” ? (sorry, i’m not non-programmer of the bunch ;) )

  • Bill Nunney

    Yep you can use booleans in that way in AS3. you can use ternary operators in PHP too :)

    I added the ‘== true’ because this article wasn’t focusing on that in particular (didn’t wanna confuse any newcomers).

  • Bill Nunney

    yep, AS3 is short for Actionscript 3 :)

  • http://topsy.com/trackback?utm_source=pingback&utm_campaign=L2&url=http://www.wuup.co.uk/as3-basics-condensing-if-statements-aka-shorthand Tweets that mention AS3 Basics: Condensing if Statements (aka shorthand) — Topsy.com

    [...] This post was mentioned on Twitter by Tammy Kahn Fennell and MarketMeTweet, PieDog Media. PieDog Media said: RT @Wuup AS3 Basics: Condensing if Statements (aka shorthand) http://bit.ly/9Ll2TZ [...]

  • Kyle

    Hey I’ve been looking around this site and have searched Google numerous times for this but i keep running in to this one problem.

    Say i have multiple buttons and they all can be clicked but have different jobs obviously and i want to only have one function for all of them so i want to pass a string with them so the function can tell which button has called it but I’m not shore how to go about this ive tried a few ways but I’m really not sure… anyway this is what i have been trying…
    [code]
    function btnPress(event:MouseEvent, identify):void {

    switch(identifier){
    case "btn1":
    //do code then break....
    case "btn2":
    //so code then break
    etc....
    }
    }
    btn1.addEventListener(MouseEvent.CLICK, btnPress("btn1");
    btn2.addEventListener(MouseEvent.CLICK, btnPress("btn2");
    etc....
    [/code]

  • Rick Cosmos

    Kyle,
    Did you ever get an answer to this because I’ve been experimenting with the same thing with no luck.

    RC

  • Bill Nunney

    Hi Kyle,

    as far as i know you cant pass data through events in that way.

    You can use the name property of the Button to achieve this:

    function btnPress(event:MouseEvent):void {

    switch(event.currentTarget.name){
    case “btn1″:
    //do code then break….
    break;
    case “btn2″:
    //so code then break
    break;
    }
    }

    btn1.addEventListener(MouseEvent.CLICK, btnPress);
    btn2.addEventListener(MouseEvent.CLICK, btnPress);

  • Bill Nunney

    Hi Rick, See solution above :)