U-X > while

 

while

Availability

Flash Player 4.

Usage

while(condition) {
	statement(s);
}

Parameters

condition The expression that is reevaluated each time the while action is executed. If the statement evaluates to true, the statement(s) is run.

statement(s) The code to execute if the condition evaluates to true.

Returns

Nothing.

Description

Action; tests an expression and runs a statement or series of statements repeatedly in a loop as long as the expression is true.

Before the statement block is run, the condition is tested; if the test returns true, the statement block is run. If the condition is false, the statement block is skipped and the first statement after the while action's statement block is executed.

Looping is commonly used to perform an action while a counter variable is less than a specified value. At the end of each loop, the counter is incremented until the specified value is reached. At that point, the condition is no longer true, and the loop ends.

The while statement performs the following series of steps. Each repetition of steps 1-4 is called an iteration of the loop. The condition is retested at the beginning of each iteration, as in the following steps:

1

The expression condition is evaluated.

2

If condition evaluates to true or a value that converts to the Boolean value true, such as a nonzero number, go to step 3.

Otherwise, the while statement is completed and execution resumes at the next statement after the while loop.

3

Run the statement block statement(s).

4

Go to step 1.

Example

This example duplicates five movie clips on the Stage, each with a randomly generated x and y position, xscale and yscale, and _alpha property to achieve a scattered effect. The variable foo is initialized with the value 0. The condition parameter is set so that the while loop will run five times, or as long as the value of the variable foo is less than 5. Inside the while loop, a movie clip is duplicated and setProperty is used to adjust the various properties of the duplicated movie clip. The last statement of the loop increments foo so that when the value reaches 5, the condition parameter evaluates to false, and the loop will not be executed.

on(release) {
	foo = 0;
	while(foo < 5) {
		duplicateMovieClip("_root.flower", "mc" + foo, foo);
		setProperty("mc" + foo, _x, random(275));
		setProperty("mc" + foo, _y, random(275));
		setProperty("mc" + foo, _alpha, random(275));
		setProperty("mc" + foo, _xscale, random(200));
		setProperty("mc" + foo, _yscale, random(200));
		foo++; 
	}
}

See also

do while, continue, for, for..in