Requirements                                                                       Required products                                                                                                                               
Prerequisite knowledge                                                        Flash Builder (Download trial)
You need to be familiar with ActionScript 3                         Adobe Animate CC    
User level

You use conditional logic every day. Do you turn left or right? Do you eat breakfast? If the line is short enough, you wait, else you come back later. These types of decisions are necessary to make it through your day and equally necessary to make any program or application you create function and interact with users.
Conditional logic is simply the written expression of these same decisions within your program. It can take a simple form, such as: if the line is short, wait. Or a more complicated one, such as: if the line is less than five people, or more than five people but moving quickly, wait. In either case, the goal is to clearly indicate the conditions under which certain actions should occur.
Within programming, those decisions are called conditional expressions. The conditional expressions you write must evaluate to either true or false . The answer to the conditional expression will determine which block of code will execute. This article will cover how to write conditional expressions and the conditional statements that use them.

if statements


The if statement asks a single question. If the answer to that question is true and only if the answer is true, then the associated code is executed. If the answer is false, the associated code block is ignored and the next statement outside of the if statement is executed. 
To write an if statement, start with if, followed by a set of parentheses around the conditional test you want to evaluate. Then use curly brackets to denote the code block you want to run if the condition is true. Look at the sample code below.
if (n>1){
 trace("Conditional expression is true"); }

Conditional expressions frequently use the equality and comparison operators: == != ===!== < <= > , and >= . You can also use the logical operators: && (logical AND) and || (logical OR). See ActionScript 3 fundamentals: Operators for more information on these operators. The code below shows the value of a variable being compared to 5 using the less than operator.
public function ageTest():void{ var age:int = Math.random()*10; if (age<5){ trace("You are too young"); } }


The code below shows logical operators being used in the conditional expressions. The first function uses AND logic. The second one uses OR logic.


public function usingANDLogic():void{ var randomNumber:int = Math.random()*10 + 1; if(randomNumber>3 && randomNumber<8 ){ trace("randomNumber is between 3 and 8. It is " + randomNumber); } } public function usingORLogic():void{ var randomNumber:int = Math.random()*10 + 1; if(randomNumber<3 || randomNumber>8 ){ trace("randomNumber is either less than 3 or greater than 8. It is " + randomNumber); } }
Because conditional expressions evaluate to either true or false and Boolean variables can only contain either true or false , you can check the state of a Boolean variable without using any comparison or logic operators. Just use the name of the variable within the parentheses following if . If the variable contains true , the associated code block executes.
public function doYouLikeProgramming():void{ var likesProgramming:Boolean = true; if(likesProgramming){ trace("I like to program too"); } }


if..else statements

Sometimes you want the program to do one thing if the conditional expression is true and another thing if the conditional expression is false. You use an else statement to execute code if the conditional expression tested by if is false. Look at the code below. Notice that a second conditionalexpression is not used with else .
 trace("Bigger than 10"); }
else{ trace("Less than 10"); }
In the code below, the variable guess is tested to see if its value is equivalent to the value of magicNumber . You would use similar code to test a value entered by the application's user.
public var guess:int = 8; public function testGuess():void{ var magicNumber:int = 7; if(guess==magicNumber){ trace("You are correct"); } else{ trace("Sorry, try again."); } }


if..else if..else statements


You can test multiple conditions using if..else if..else statements. Once a condition evaluates to true, the code in that condition's block is executed, and the if..else if..else statement is exited.
The value of the variable candyPieces is tested in the code below. The first if statement checks to see if the value is less than or equal to 3. If it is, "You only have a few pieces" appears in the output window, and the if..else if..else statement is exited. If the value is not less than or equal to 3, candyPieces is checked to see if its value is less than or greater than 10. Again, if the answer is true, the associated code if executed. If the answer is false, the next test is run. If none of the conditional statements evaluate to true, the code in the else block is executed.
public function howMuchCandy():void{ var candyPieces:int = Math.random()*50; if(candyPieces <= 3){ trace("You only have a few pieces"); } else if(candyPieces <= 10){ trace("You have a handful of candy"); } else if(candyPieces <= 25){ trace("You have some candy"); } else if(candyPieces <= 35){ trace("You have lots of candy"); } else{ trace("You have lots and lots of candy."); trace("Can I have some?"); } }


switch statements


If you have a set list of possibilities and each one has a different outcome, you can a use switch statement rather than a series of if..else if..else statements. 
Each possible outcome is a case in your switch statement. Each case must end with break statement. In if statements only the code enclosed in the curly brackets {} is executed when a condition is true. switch statements do not use curly brackets so break is used to stop the conditional statement from proceeding to the next case  and exit the switch statement.
Although it is not required, it is good practice to use a default case to exit the switch statement in  case none of the possible outcomes occur. If none of the cases are true, the default case will execute, preventing an infinite loop. The default case must be the last option so the switch statement is not exited before all cases are tested.
In the example below, the variable grade is tested against the possible grades a student could earn. Remember that when you work with string values, they are always in quotes. Thus, the value for each case is in quotes.
public function gradeSwitch():void{ var grade:String = "D"; switch(grade){ case "A": trace("Great job - you are getting an A"); break; case "B": trace("good job - you are getting a B"); break; case "C": trace("average - you are getting a C"); break; case "D": trace("work harder - you are getting a D"); break; case "F": trace("I'm sorry - you are failing"); break; default: trace("Invalid data"); break; } }


Where to go from here


Conditional logic can be expressed in multiple forms, but all forms have the same goal: to clearly indicate the conditions under which certain actions should occur. Conditional logic is a core technique required to develop programs and applications of any complexity. Conditional expressions are often used in combination with loops and to search for data in objects, arrays, vectors, and XML data (coming soon).