Mel - Maya Embedded Language

<HOME>   <PREVIOUS>   <NEXT>

Loops and If Blocks - while loop

if then else
for
for in
while
do while
switch

while

The while loop runs commands while the test condition is true.
A while loop mainly consists of:

while (test condition;)
{
     do something;
     change the test condition;
}


string $myArray [ ] = {"one","two","three","four"};
int $i =0;
while ( $i < size($myArray))
{
     if ($myArray[$i] == "one")
          print ("\n"+$myArray[$i] + " dead roach!");
     else
          print ("\n"+$myArray[$i] + " dead roaches!");
     $i++;    //do not forget this or Maya will get stuck in an infinite loop
}

returns:

one dead roach!
two dead roaches!
three dead roaches!
four dead roaches!

Exercise: Use a while loop to create a random number of polyPlanes each with a unique name. Move each plane to a random x,y position and rotate them randomly along one of the axis.
*Don't forget that you must be able to get out of the loop. Create a variable that is set randomly and if the random number it selects is greater than a certain number, it will exist the loop.