Conditionals and Loops

Using conditions and loops, you can teach your Flash animations how to make decisions, and viewers will think both you and your animations are very smart. ActionScript provides a few different decision-making statements that you can use depending on your needs. In human-ese, they work like this:

Those three forms of decision-making may seem very similar, but as you'll see in the following explanations and examples, there are some important basic differences. You'll hear programmers refer to these decision-making statements using different terms, like program flow controls, conditionals, and loops. These tools have one thing in common: They all help you dictate whether or not ActionScript runs some specific lines of code.

Two of ActionScript's statements tackle the "If (this condition exists) do (these actions)" situations. The first and simplest state is appropriately called an if statement.

There's a good rule of thumb to remember with computers. If you're doing the same chore over and over again, there's probably some way your computer can to do it more efficiently. Computers are great at repetitive tasks, and that's certainly true of ActionScript, which has two great ways to repeat statements in your programs. Using the while() statement, you can have your computer repeat a task as long as a certain condition exists. Using the for() statement, you can tell your computer exactly how many times to repeat a task.

The while() statement checks to see if a condition is met. If it is, then the code within the curly brackets runs. If the condition isn't met, then ActionScript moves on to the next statement. It's very common to use the while() statement with a variable that's incremented. So, if you want your assistant to pop down to the auto store and buy six Stutz Bearcats (enough for you and a few close friends), you'd put together a statement like this:

while (myStutzBearcats < 6) {
 buyStutzBearcat();
 myStutzBearcats = myStutzBearcats + 1;
}

Like the if() statement, the condition for the while() statement is inside parentheses, and if the condition is met, then the statements inside curly brackets {} run. In this example, the second line runs a function that buys a Bearcat. The third line increments the variable that keeps track of how many Bearcats you own. As long as that number is less than 6, the program loops; you buy another and add 1 to the number of Bearcats you own.

The whole idea of incrementing a value, like myStutzBearcats, is so common that there's even a shorthand way of adding one to a variable. The third line could also read:

myBearCats++;

There's no assignment operator (=), but the statement is assigning a new value to myBearcats. When you want to count down, you can use the decrement operator (--). Here's a statement that sells off your Stutz Bearcats until you have only three left.

while (myStutzBearcats > 3 {
 sellStutzBearcat();
 myStutzBearcats--;
}

The for() loop gives you a very compact way to repeat a portion of your program. The mechanics that make a for() statement run are all packed in the first line. It's so compact and down to business that it reads a little more like machine talk than human talk. Here's an example:

for (var myStutzBearcats:Number = 0; myStutzBearcats < 6; myStutzBearcats++)
{
 buyStutzBearcat();
}

Like the earlier statement, this loop buys six Stutz Bearcats. A lot goes on in that very first line of code. From left to right, here's what happens:

  • The word for indicates a for() statement.

  • The beginning parenthesis is your clue that the condition follows.

  • The word var means that a variable is being declared.

  • myStutzBearcats:Number is the name of that variable and its data type.

  • The assignment operator (=) immediately gives the variable a value of zero.

  • Following the semicolon, you finally arrive at the condition that's being tested: Are there fewer than six Bearcats?

  • The semicolon ends that statement, and in the next myStutzBearcats is incremented.

The for() statement is very compact, and it puts everything you need to know right up front. Script writers often create for() statements using the variable i for the condition (see the note on Note). You'll often see something like:

for (var i = 0; i < 6; i++) {
 buyStutzBearcat;
}

Just like the earlier for statement, this one purchases six of those beautiful Bearcats.