ActionScript Programming/PartII/Chapter 4

Conditions/Loops (part 1)


Conditions and loops are one of the most important things not only in ActionScript but also in all programming languages, because they are used everywhere. For understanding these conditions lets discuss each of them one by one.


If, else if, else


“if” is used to compare two or more expressions. For example you have an “Input Text” and a button. You want to check if the value of “Input Text” equals “hello”. To do that you must use “if”. The use of “if” is the following:

 if ( <condition> ) {
    //here goes your code

The code in “if” will be executed when the condition which you write is true. But for example you wrote a condition which was false, and you want to know about it. In this case you can use “else”.

 if ( <condition> ) {
    //here goes your code
 } else {
    //here goes your code

But before using any condition you must know comparison and logical operators.

Operators (part 2)


Comparison operators

Operator Description


This operator returns true if two expressions are the same, and false if they aren’t. If their data types are different, then they are converted. For example “5” != 5 returns false thou “5” is a string and 5 is a number.


This operator is the same as “!=” the only difference is that data types are not converted.


Compares two expressions and determines whether expression1 is less than expression2. If so, the operator returns true


Compares two expressions and determines whether expression1 is less than or equal to expression2 ; if it is, the operator returns true


This operator checks whether expression1 equals expression2, returns true if so and if data types are not the same, then they are converted.


This operator is the same as “==” but in “===” data types are not converted.


Compares two expressions and determines whether expression1 is greater than expression2 and returns true if so.


Compares two expressions and determines whether expression1 is greater than or equal to expression2 and if so it returns true.

Logical operators

Operator Description


Inverts the Boolean value of a variable or expression.


The operator “&&” means “and”. For example you can join two conditions with it.


The operator “||” means “or”. It can be used for joining two conditions.

Miscellaneous operators

Operator Description


This operator subtracts 1 from the expression. For example you have “a” variable and want to decrease its’ value by one. Instead of writing “a-=1” or “a=a-1” you can write “a--“.


This operator is the same as “if” condition. The following example will demonstrate how it works:
 var x = new Number(5);
 var y = new Number(10);
 var z = new Number();
 z = (x < y) ? x: y;
The last line means that if “x<y” then “z=x” else “z=y”. You can write the same using “if”.
 if (x < y) {
 	z = x;
 } else {
 	z = y;


This operator adds 1 to the expression. For example you have “a” variable and want to increase its’ value by one. Instead of writing “a+=1” or “a=a+1” you can write “a++“.


Discussed later


The “typeof” operator evaluates expression; the result is a string specifying whether the expression is a string, movie clip, object, or function. For example you have a button named “mybutton”. If you write this code:

then the traced value will be “button”. If the object is Movie Clip then the return value is “movieclip”, if it is a string the return value is “string”.


Discussed later

Now when you know these operators we can continue learning conditions and writing new programs.

Conditions/Loops (part 2)


Lets write a program which will check if the values of two Input Texts are the same. First of all create two Input Text and set first ones var to “input1” and the second ones to “input2”. Then add a button and set its name to “button”. Add the following lines to the actions of “button1”:

 1.   on (release) {
 2.      if (input1 == input2) {
 3.         trace(YES);
 4.      } else {
 5.         trace(NO);
 6.      }
 7.   }

In line 2 we check if input1 equals input2 using “if” and operator “==” (Note: don’t confuse “=” with “==” because they are different operators. “=” assigns value to the variable, but “==” only checks two expressions. For example if you have “if” and you want to check if any variable “a” equals “hello” then you must write “ a==”hello” ” in the condition. If you write “ a=”hello” “ then a will become “hello”.) and if so then we trace “YES” else we trace “NO”. But now imagine another situation. You have an Input Text and a button and when the user type his/her name in the Input Text and clicks the button you want to trace different values. To solve this you must add the following code to the button:

 1.   on (release) {
 2.      if (input1 == Jane) {
 3.         trace(Hello Jane!!!);
 4.      } else if (input1 == Ben) {
 5.         trace(Hello Ben!!!);
 6.      } else if (input1 == John) {
 7.         trace(Hello John!!!);
 8.      } else {
 9.         trace(Hello unknown!!!);
 10.     }
 11.  }

If you want you can write a simple program just for you. You can create two Input Texts one button, a Dynamic Text (that will show the status) and write a code to determine if the user who want enter your program is member or not. This little program will be your security, keeping other people away. Below is shown a little fragment of the button’s actions:

 1.   on (release) {
 2.      if (UserName == user1 && Password == password1) {
 3.         //here goes your code
 4.      } else if (UserName == user2 && Password == password2) {
 5.         //here goes your code
 6.      } else {
 7.         Status = Invalid Details!;
 8.      }
 9.   }

UserName is the “var” property of the first Input Text Password is the “var” property of the second Input Text Status is the “var” property of the Dynamic Text

“For” is used to repeat the code for several times. Below is shown the use of “For”:

 for ( <init>; <condition>; <next> ) {
    //here goes your code

Now I will bring you example where you need to use “For”. For example you want to add all numbers from 1 to 10 (1+2+3+4+…+10). For doing it you have to write the following code:

1.   var i = new Number();
2.   i += 1;
3.   i += 2;
4.   i += 3;
5.   i += 4;
6.   i += 5;
7.   i += 6;
8.   i += 7;
9.   i += 8;
10.  i += 9;
11.  i += 10;

(or if you are too smart)

 1.   var i = new Number();
 2.   i = 1+2+3+4+5+6+7+8+9+10;

But, anyway, it’s boring to write such a code. Instead you can use “For” and repeat the code for 10 times:

 1.   var i = new Number();
 2.   for ( e = 1; e <= 10; e++) {
 3.      i += e;
 4.   }

In the first line we declare i number variable. In line 2 we begin our For. e = 1 is the <init> part. In the init part you assign value to the variable. In our case we assign 1 to variable e. Now you must ask why I use the word variable with e. Because when you init the value Flash automatically makes e a number variable. If you declare e as a number variable in the beginning of the code there will be no problem. You can write the following example like this:
<syntaxhighlight lang="actionscript3">
 1.   var i = new Number();
 2.   var e = new Number();
 3.   for ( e = 1; e <= 10; e++) {
 4.      i += e;
 5.   }

The second part of “For” <condition> tells Flash how much to repeat the action. In our case we wrote to repeat the action while e is less than or equal to 10. So when “e” becomes 11 it will stop repeating the action. In the third part of “For” is <next> you must tell Flash how to change the value of the variable each time it repeats the action. In our case we wrote “e++”. This means that each time Flash repeats the action it will increment e by one. In the actions of “For” (line 4) we wrote “i += e;”. This means that we will add “e” to “i” each time the action repeats. So this small code will give the sum of numbers from 1 to 10. You can trace the value of “i” at the end of the code to see its value. It must be “55”.

 1.   var i = new Number();
 2.   var e = new Number();
 3.   for ( e = 1; e <= 10; e+=2) {
 4.      i += e;
 5.   }

This example does the same as the previous one, but this time we increment the value of “e” by 2, not by one, as in the previous example. So our example will count the sum of the following numbers: “1, 3, 5, 7, 9”.

For .. In


“For .. In” is used to loop through the properties or objects of the object you specify. The following is the structure of the statement:

 for ( <variableIterant> in <object> ) {
    //here goes your code

Now lets understand what, where and how can this statement be used. For example, if you want to get the list of objects in “_root” object you must write the following:

 1.   var e = new String(Hello!!!);
 2.   for (obj in _root) {
 3.      trace(obj);
 4.   }

If you write this code in the actions of the frame and run it, the output value will be:


The first line of the code declares a new string variable named “e”. Then the program loops though each element in “_root” object and traces its name. You already know that each local variable declared in timeline is an element of “_root” object like “e”, but what is “$version”? “$version” is a built-in string variable which keeps the name and the version of the operating system the movie is run at.

« Previous    Next »