Bunny

Programming Primer
Licensing And Preface

Standardized Coding Practices

Errors And Exceptions

Variables

String DataType

Number DataTypes

Conditionals

Commonly Used Flash Commands

More Topics:

3D

Music

Processing

Flash



Part 7: Conditionals (Controlling The Flow Of Execution)

Controlling The Flow Of Execution

When we write a script, generally we don't want the script to do the same thing every time we use it. Often what we want is a script that is versatile in it's application. Excluding his versatility would make the program predictable and boring. We therefore use certain built-in commands that help us control the way in which our programs run, allowing us to skip blocks of code and execute other blocks of code before others in a non-linear way. This can be done in a dynamic and changing sequence based on the users input. The process of creating this dynamic ordering (by allowing us to execute code in a non-linear order) is what is referred to as controlling the flow of execution.

The if Statement

Branching is the process of choosing one flow of execution over another based on the way the code is structured. The if statement allows us to create programs with the ability to branch off in different directions based on dynamic or predetermined values.

All if statements have a condition. A condition will either evaluate to true of false. The value of this condition determines which route your code will branch off into.

if(value1 == value2){ |This is a conditional
   //if the condition evaluates to true
 
  //Flash will execute this code block
   trace("value1 is equal to value2");
|
|1st Branch
|
}else{
   //if the condition evaluates to false
  
//Flash will execute this code block
  
trace("value1 is not equal to value2");
|
|2nd Branch
|
}

Comparisons and Comparison Operators

The conditions of an if statement are created by comparing values against each other. These values are compared using comparison operators. A list of Flash's comparison operators follow,

== is equal to

!= is not equal to

> is greater than

< is less than

>= is greater than or equal to

<= is less than or equal to

Comparison operators work by comparing what is on their left side to that which is on their right side then returning a value of either true or false based on this comparison. For example;


if(5>4){

trace(“true”);

}

The comparison in an if statement is enclosed between parenthesis. This above example is literally read in plain English as,

“If five is greater than four, trace true.”

What this simply means is that when this code is executed Flash will check if the value on the left side of the comparison operator is greater than that on the right of the operator. If the value on the left is greater than the value on the right (and as you are aware 5 is indeed greater than 4), Flash will return a value of “true”. Since the condition has evaluated to true flash will go ahead and execute the code in the curly brackets directly after the condition, in this case telling Flash to print the characters “true” in the output window. However if the condition evaluated to false flash would skip the code in the curly brackets and proceed to the next block of code following the closed curly bracket. Since there is no code following the closed curly bracket in this example Flash will simply evaluate the condition, realize that it equates to false and simply skip the trace command then do nothing, there after. Therefore by using a condition in our if statement we are controlling the flow of execution of our script and controlling what Flash eventually outputs to the user.

Please also note that the comparison operator == (is equal to) is not to be confused with the assignment operator = (equals). The former checks if the value on the left matches the value on the right of the operator, the latter assigns the value on the left, of the operator, to that which is on the right.

It's not only numbers that can be compared but expressions, variables and strings can also be compared. For example the if statement that follows compares two strings and returns a value of either true or false based on the strings position in the English dictionary

 

if(“apple”< “orange”){

trace(“apple comes before orange in the dictionary!”);

}

It is not common to make a comparison of this nature in scripting as you can see it's application is quite specific eg. perhaps the sort of thing that would be used in a spelling game. The condition has evaluated to true because “apple” is numerically less than “orange”, ie. comes before “orange” in the English dictionary.

The Structure of the if Statement


if (variable01==variable01){

trace(“variable01 is equal to variable01”);

}

We have already seen how this script will execute, variable01 will be compared to itself and will obviously be the same, therefore return a result of true. This means the following code block between the curly brackets will execute. This is however pretty linear in terms of controlling the flow of execution. What if we could have our computer make a logical decision based on the condition provided then have two possible scenarios, one that is executed if the condition evaluated to true and another if the condition evaluated to false. We can with an if-else statement.


if (variable01==variable01){

trace(“variable01 is equal to variable01”);

}else{

trace(“somethings not right!”);

}

By adding the else clause to our if statement we have now provided our mini-application with two possible scenarios. If the condition evaluates to true the first block of code will be executed. If, however, the condition were to evaluate to false the first block of code between the first set of curly brackets would be ignored. Flash would then proceed to the else clause (if it exists, in this case it does) and execute the code within the following set of curly braces (in this case telling Flash to print the text “somethings not right!” to the Output Window.

The if-else if statement

Using an if-else statement can be useful if you are only checking for one of two possible values. However what if you wanted to check for a specific value amongst many possible values. Using the else if clause you can check for as many different values as you like in the same if statement and provide an independent branch of code for each possible value. This is because each else if clause has it's own condition that can evaluate to true or false and Flash can subsequently choose which code block to execute. The else if clause is contained within the if statements curly brackets and it's syntax closely resembles that of the if statement itself.


if(variable01 == variable01){

   trace(“variable01 is equal to variable01”);

}else if (variable01 == variable02){

   trace(“variable01 is equal to variable02”);

}else if(variable01 == variable03){

   trace(“variable01 is equal to variable02”);

}else{

   trace(“variable01 could be not be defined”);

}

Adding else if clauses can be effective in capturing various possible values and useful for debugging a script.

The if-else if statement does not have to end with an else clause, but it can be useful to present a final set of instructions to the program in the case that none of the other clauses evaluate to true.

It is possible for an if statement with several clauses to have more than one condition evaluate to true. In this case it is only the first condition, that is read from the top of the script down, that will have it's associated code block executed. All other conditions whether they evaluate to true or false will have their associated code blocks ignored. It is worth checking that your scripts do not fall into this trap. For an example of the else-if if statement check out the Guess my number Game.

Compound Conditions

Compound conditions allow you to have more than one condition within the same parenthesis in the same if statement's conditional and have your program make a decision based on a result accumulated from all conditions within the if statements conditional.

Compound conditions are combined with logical operators and involve two or more comparisons in the same condition.

The most commonly used logical operators are && and ||

The first is referred to as “and”.

All simple conditions within the same parenthesis, separated by the “and” logical operator must equate to true in order for Flash to return a value of true for the compound condition. If any one or all of the simple conditions are false, Flash will return a value of false for the compound condition.


var value1:Number = 1;

var value2:Number = 2;


if(value1 == value1 && value2 == value2){

trace(“value1 is equal to value1 and value 2 is equal to value2”);

}

The second logical operator || is referred to as “or”.

In order to get a value of true returned for a compound condition using several simple conditions, within the same parenthesis and separated by the “or” logical operator, at least one of the simple conditions in this scenario must equate to true.


var value1:Number = 1;

var value2:Number = 2;


if(value1==value2 || value1 == value1){

trace(“value1 is either equal to value1 or value2”);

}

Flash will only return a value of false if all simple conditions in the above mentioned scenario equate to false.


var value1:Number = 1;

var value2:Number = 2;

var value3:Number = 3;


if(value1==value2 || value2 == value3){

trace(“true”);

}else{

trace(“false”);

}


See passwordProg for an example of compound conditionals.
Download the passwordProg.zip file (150.5 KB pwProg.FLA, pwProg.SWF You will need Flash 8 Pro + to open these files)


Password Program


Next: Commonly Used Flash Commands