What is a condition and why is it useful?
If you think about your every day life, you use conditional logic all the time. Example: If I have something I can contribute to the thread then I will post a reply (okay, so mayb not all of us). Similarily, Flash can test if something is true or false and then execute different commands based on that. This could be used to test if a password is correct, or if a movie clip has a certain proprety.

if()
In basic programming languages, "then" usually accompanies each and every if. In Flash, the then is implied. The if operator takes one parameter (the thing inside the parenthesis.) The parameter is a boolean (variable that can only be true or false). If that parameter is true, then Flash executes the code between the curly brackets. If its false then it doesn't. Example:

This tests to see if the variable userInput is equal to the string bologna. In the above case it returns true and the line "I could use a sandwich" is displayed in the output screen. If you change user input to say something like turkey, then the sentence won't be displayed.

<else>
Else can only occur once per if statement. Else tells Flash what to do if the parameter in the if statement is false. Example:

This will display the line "I prefer bologna." in the output window because "ham" doesn't equal "bologna". If you change userInput back to "bologna" then you should see the first line shown again instead.

else if ()
Else if allows you to make multipath loops. If Flash determines the initial if statement to be false then it begins testing the else ifs until it finds one that is true. Example:

This will test if the grade is greater than or equal to the grade needed for the grade letter. If it is true, then it will assign that and skip the rest of the if/elseif/else statement. If it gets all the way down without finding an answer it will give the grade an F.

switch, case, default and break
I am lumping all of these together, because without one another they are relativly useless. When used together they can accomplish some basic things:

Note the colons instead of the semicolons after each case statement. You can test around with this. The break statements skip out of the switch as soon as one of the conditions is met. In this case you could remove it and there would be no noticable difference.

What is a loop and why is it useful?
A loop is a set of code that gets executed several (or even thousands of) times in a row. There will be some variable in the loop that changes each time. They are useful for hundreds of things. A few examples would be: testing your character against multiple platforms in your newest platformer, testing for multiple correct passwords, testing pratically anything that you have more than one of.

while()
While is the simplest loop there is. It's pretty much self explanatory, but I'm going to explain it anyway. While takes one parameter. This parameter is a boolean. The loop executes whatever is in the block of code while the parameter is true. Example:

If you copy and paste this into a frame, you will see that Flash runs through the loop until the myNumber <= 100 returns false.

do ... while
This is almost exactly like the code above with a slight difference: The loop is executed and then it checks if it should go back and do it again. So, using a similar example to above to illistrate the difference:

You will see that the number is increased by 5 even though it was already above 100. This is because the do ... while loop will always be called at least once.

for()
For loops are my favorite because they are specialized while loops that I often find more use for. The for loops requires three parameters. The first is an initiation spot. The second is the condition. Third is what Flash does each time it completes a loop. For example (no pun intended):

for (x=0; x <= 100; x++) {
trace(x);
}

How Flash reads this: First Flash sets x equal to 0. It then tests x <= 100 and returns true so it runs all of the code between the curly brackets (in this case trace). After that code is done, Flash increments x (adds one to x). Flash then tests if x <= 100 again and then runs the code again and then increments and tests and so on.

At 3/18/06 06:56 AM, Flame_Reaper wrote:
it didnt work. is it because of my code or is the problem that it just cant be done?

There's no built-in way to check it, to my knowledge, but you could easily make a workaround. One way is to double click the ball's icon in the library to edit it. Then let the first frame of the movie clip have the action:

At 6/28/05 11:47 AM, BleeBlap wrote:
A loop is a set of code that gets executed several (or even thousands of) times in a row. (...) A few examples would be: testing your character against multiple platforms in your newest platformer

How do you that anyway? Test your character against thousands of platforms? Ok, so say that my characters name is 'player' and my platforms are called 'floor' (or 'floor1', 'floor2', etc if neccessary), what would the code be? I need to know quick and any helpful response would be appreciated!

About break statements: There would definitely be a noticable difference if you left the break statements out of that switch. Switches are designed so that they won't exit a case as soon as another one is encountered, meaning, without the break statement, it'll keep going down the list. In your example, that would result in multiple trace statements.

This element of switches can be useful though, like if you want the same function to execute for different values of a variable, all you have to do is stack the cases on top of each other, like so:

You can use that method too if you don't want something to be case-sensitive, like

case "a":
case "A":
trace("You input 'A'");
break;

Another use for breaks is early exit out of loops, like you want to cycle through an array, but only until you find an element with a certain property, you could use break to terminate the loop as soon as that element is found.

At 6/28/05 11:47 AM, BleeBlap wrote:
A loop is a set of code that gets executed several (or even thousands of) times in a row. (...) A few examples would be: testing your character against multiple platforms in your newest platformer

How do you that anyway? Test your character against thousands of platforms? Ok, so say that my characters name is 'player' and my platforms are called 'floor' (or 'floor1', 'floor2', etc if neccessary), what would the code be? I need to know quick and any helpful response would be appreciated!

You could push each floor element into an array and then cycle through that array every frame, but I find that sloppy and it soaks up processor time. What I would do is put EVERY floor element into a single movieclip, and then use a shapeFlagged hitTest at the x and y location of the player.

I think that switch is better for many conditions because once it finds the correct case, it immediately executes the command. If...then on the other hand, needs to test every single line of the statement until it executes one particular statement.