Loops


Loops

The next important programming feature is called looping. A loop is similar to a conditional with one major difference: A conditional tests a condition to see if it's true and then either runs a block or skips the block, depending on the outcome of the test. A loop, on the other hand, tests a condition, and if it's true , it runs a block of code just like the conditional. When the block of code is finished, however, the loop comes back to the condition and tests it again. It will continue to do this, running its block of code over and over, until the conditional evaluates to false .

The while Loop

The while loop is the primary loop type. The other kinds of loops are just variations on this standard form of loop. Much like a conditional, there is a keyword ”in this case, while ”followed by an expression in parentheses, followed by a code block. Let's look at an example now:

 myNumber = 1; while( myNumber < 99 ){     trace(myNumber);     myNumber++; } 

This bit of script sets up the myNumber variable with a value of 1. Then there is a while loop that has myNumber less than 99 as its condition. The condition is followed by a block of code that outputs the value of myNumber and then increments it. When the code is executed, the while loop first tests its condition. It evaluates to true because 1 is less than 99, so the block executes, printing myNumber . myNumber is then incremented to 2 and the block ends. At this point, the program jumps back up to the while condition and tests it. Again, the condition tests true because 2 is greater than 99, so the code block executes. This cycle repeats until myNumber is finally incremented to 99, at which time the condition tests false and the block of code is skipped . Take a look at Figure 2.20 to see a flowchart of the while loop.

click to expand
Figure 2.20: A while loop executes its block of code repeatedly until its condition tests false .

The do while Loop

A do while loop is almost identical to a while loop, except that in a do while loop, the code block is executed once before the condition is tested . In this way, you are assured that the code block will execute at least once.

In this style of loop, the code block immediately follows the do keyword. After the code block is the while keyword, followed by the condition. Consider the following script:

 myNumber = 99; do{     trace(myNumber); }while(myNumber++ < 10); 

Regardless of what you initialize myNumber to be, trace will execute once. After that, it depends on the test condition. I also added a small twist by incrementing myNumber inside the condition, just so you get used to seeing things like that. It's perfectly valid to put complex expressions inside conditions. Sometimes loops are even built with no script in the block; instead, the condition statement does all the work.

Caution  

In a do while loop, a semicolon must terminate the final while statement with condition. This is not necessary in a conditional statement or regular while loop, but it is required in a do while loop.

Have a look at Figure 2.21 to see a flowchart of the do while loop and compare it to Figure 2.20.

click to expand
Figure 2.21: A do while loop always executes its code block one time before testing the condition.

The for Loop

We now come to the last loop type: the for loop. This is yet another variation on the while loop, and it operates in a similar manner. There are a few extra details that accompany the for loop, so let's take a look at the structure of it first. Its general form is as follows:

 for(  expression1  ;  expression2  ;  expression3  ){} 

You can see that this loop begins with the keyword for , followed by three expressions in parentheses, separated by semicolons. The code block follows the closing parenthesis. The key to a for loop is in those three expressions inside the parentheses. The first expression is executed once before the for loop begins. Then the loop becomes identical to a while loop, with the second expression being the loop condition. Finally, the third expression is executed as the last thing in the code block before the condition is retested. Let's look at some code that shows a common use of the for loop:

 for(myNumber = 0; myNumber < 10; myNumber++){     trace(myNumber); } 

The first expression is executed once before the for loop begins, setting myNumber to a value of 0. Then the second expression is used as a while loop condition, so it is tested. Because 0 is indeed less than 10, the code block is executed. Finally, before the execution point jumps back to the condition to test it, the third expression is invoked, incrementing myNumber . You should be able to tell that this loop prints the values 0 through 9 to the Output panel.

Take a look at Figure 2.22 to see a flow chart of the for loop and compare it with the previous two flowcharts.

click to expand
Figure 2.22: A for loop is a handy way to execute a code block a specific number of times.
Note  

Notice that the for loop uses semi-colons to separate the three expressions. That's because it's actually full lines of code that go inside there. The semicolons are necessary so that Flash knows where one expression begins and the others end.

Tip  

When you are choosing which type of loop to use, consider this. A while loop is generally used when you don't know how many times the loop needs to iterate. A for loop is used when you know exactly how many times the loop must iterate. This is a convention, but not a rule. You'll see me and other programmers break this rule of thumb from time to time.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net