Please note that ExpatTech is close for the Christmas and New Year holidays. We will re-open on 2nd January 2024.

ExpatTech Techblog

Peter Todd 2011.01.17. 12:06

Flash Cookbook - Tricks - Conditional chains

If you have a conditional, like:

var temp:String = "something";
if(temp == "1") do1();
else if(temp == "2") do2()>
else if(temp == "3") do3();
else doElse();

or written with switch:

switch(temp)
{
   case "1":
      do1();
   break;
   case "2":
      do2();
   break;
   case "3":
      do3();
   break
   default:
      doElse();;
}

you can still find a thirds way, what actually compacts it aswell:

temp == "1" ? do1() : temp == "2" ? do2() : temp == "3" ? do3() : doElse();

Note, if you want to add more options, after the else (:) you put another start branch (?) and so on.

(if) temp == "1" ? do1() : (elseif) temp == "2" ?  (elseif) do2() : temp == "3" ? (elseif) do3() : (else) doElse();