For in ActionScript


for is ActionScript’s looping construct. Here are some basic types of for loops.

Example 1: Basic for loop with a single condition

The most basic type, with a single condition.

var i:int = 1;
while (i <= 3) {
    trace(i);
    i = i + 1;
}

Example 2: Classic initial/condition/after for loop

A classic initial/condition/after for loop.

for (var j:int = 0; j < 3; j++) {
    trace(j);
}

Example 3: Iterating N times using range

Another way of accomplishing the basic “do this N times” iteration is using a for loop over a range of numbers.

for (var i:int = 0; i < 3; i++) {
    trace("range", i);
}

Example 4: Infinite loop until break

for without a condition will loop repeatedly until you break out of the loop or return from the enclosing function.

while (true) {
    trace("loop");
    break;
}

Example 5: Continue to next iteration of the loop

You can also continue to the next iteration of the loop.

for (var n:int = 0; n < 6; n++) {
    if (n % 2 == 0) {
        continue;
    }
    trace(n);
}

Output

Here’s what you would see when running these examples:

1
2
3
0
1
2
range 0
range 1
range 2
loop
1
3
5

We’ll see some other for forms later when we look at range statements, channels, and other data structures.

查看推荐产品

Comments powered by Disqus