# 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++“. |
| instanceof | Discussed later |
| typeof | 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: trace(typeof(mybutton)); 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”. |
| void | 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”:
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:
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:
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

“For” is used to repeat the code for several times. Below is shown the use of “For”:
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:
(or if you are too smart)
But, anyway, it’s boring to write such a code. Instead you can use “For” and repeat the code for 10 times:
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”.
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:
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:
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.