Loops

 

Loop statements repeatedly execute statements while a certain test condition is true.

Although looping allows you great control and power in writing scripts, it can also be a source of problems. Loops that endlessly repeat execution of statements will do just that-never end. For example, executing "while (1);" will loop nothing forever. Maya will never stop executing this line; don't try it unless you are prepared to quit Maya.

This section describes the three main loop statements, while, do-while, and for. The loop interruption statements, continue and break, are also described.

 

while

A while loop continues to execute some designated statements, as long as a certain condition is true. It has the following format:

while (test condition)
    statement;
  

If test condition is true, Maya executes the statement. Maya then evaluates the test condition again. If the test condition is true, it executes the statement again. This process continues until the test condition is false. At that time the loop exits.

Example

int $files = 5;
while ($files > 2)
    print("There are " + $files-- + " files left.\n");

The above example produces the following output:

There are 5 files left.
There are 4 files left.
There are 3 files left.

When the condition is first evaluated in the above example, files has the value of 5 which creates the true condition that 5 is greater than 2. Therefore, the print statement is executed, which also decrements files to a value of 4.

At this point, the test condition is evaluated again with a value of 4 for files. The test condition is again true so the print statement is executed, decrementing files to a value of 3. Again the test condition is evaluated to be true, and so the print statement is executed. Finally, with a value of 2 for files, the test condition is false, since 2 is not greater than 2. The while statement exits.

 

do-while

A do-while loop is very similar to a while loop because both execute some specified statement as long as a certain test condition is true. However, the do-while loop first executes the statement and then checks whether or not to repeat the loop. This ensures that the statement is executed at least once.

A do-while loop has the following format.

do    
    statement;
while (test condition);

Note that the do-while loop must terminate with a semicolon (;).

Example

do {
    print("Here comes the rain again.\n");
    print("Raining on my head like a memory.\n");
} while (rand(2));

 

for

Often you may want a loop control that has initialization, test, and an increment statement. For this you can use the for loop. The for loop has the following format:

for (initializer; test condition; incrementor)
    statement;

The initializer is executed only once, before the rest of the for loop is executed. The initializer is a statement that is generally used to assign a value to a variable in the test condition. You can use the initializer to execute any statement or you can omit the initializer entirely. However, it cannot be used to declare a variable.
The for loop evaluates test condition before executing statement. This part of the for loop is exactly like the while loop. In fact, a for loop is just a while loop with an initializer and incrementor. However, if the test condition is missing, the test condition is assumed to always be true, which would create an error in a while loop. While the test condition is true, the statement is executed; as soon as it is evaluated to be false, the loop is exited. The incrementor is executed at the end of each iteration of the loop, as if it were added to the end of the loop. Typically, the incrementor is used to increment some loop-control variable, although you can use it for any statement or omit it entirely.

Example

for ($xxx = 0; $xxx < 3; $xxx++)
    print("Hit me three times.\n");

The for loop also allows you to use multiple initializers or multiple incrementors. To do this, separate each initializer and incrementor with a comma (,).

continue

The continue statement works inside all loops. It forces the next iteration of the loop to occur, skipping any remaining statements in the loop.

Example

float $fox = 5;
for ($fox = 0; $fox < 5; $fox++) {
    print("$fox equals: " + $fox + "\n");
    if ($fox > 2)
    continue;
    print(" Got here\n");
}

The output is:

$fox equals: 0
 
Got here
$fox equals: 1
 Got here
$fox equals: 2
 Got here
$fox equals: 3
$fox equals: 4

Without the if statement, the loop would also output Got here after the lines $fox equals: 3 and $fox equals: 4. Maya ignores the continue statement until $fox increases to a value greater than 2. When $fox becomes 3 or greater, the continue statement executes and skips the remaining statement in the loop, the statement that prints got here.

break

The break statement exits a loop immediately. You can use a break statement inside all loops.

Example

float $free = 0;
while ($free < 10) {
    print("$free equals:  " + $free + "\n");
    if ($free++ == 3)
        break;
}
print("I'm free!");

The output is:

$free equals:  0
$free equals:  1
$free equals:  2
$free equals:  3
I'm free!

Without the if statement, the loop would execute 10 times and display the numbers 0 through 9. The break statement terminates the loop after $free is equal to 3, so the print statement in the loop is only executed four times.

This website has been archived and is no longer maintained.