Loops 2

There are several other types of loop in Mel beside the for loop shown on the first loops page.
The while loop is the simplest loop.  It takes a single conditional statement.
As long as the condition is true, the loop will continue to execute.

{
	int $i = 0;
	while ($i < 100)
	{
		print ($i + "\n");
		$i++;
	}
}

Easy enough. In this case a for loop would have probably been the best choice.
While loops are the easiest to create an infinite loop with.
For example, if you enter this in the script editor you will crash Maya.

	while(1);

1 equals yes, so that loop will never end, nor will it actually do anything except freeze Maya.

The break command:

The break command ends a loop before its condition evaluates false.
Suppose you want to see how long it takes a random number generator to come up with a certain number.
This example also uses conditional statements and the == operator.

{
	int $number;
	int $stoppingNumber = 17;
	while(1)
	{
		$number = rand( 0, 20 );
		print ($number + "\n");
		if ($number == $stoppingNumber)
			break;
	}
}

The Continue command:

Besides the break command there is also the continue command.
Suppose in the last example we wanted to ignore any number smaller than 10. 
We could use continue in this way:

{
	int $number;
	int $stoppingNumber = 17;
	while(1)
	{
		$number = rand( 0, 20 );
		if ($number < 10)
			continue;
		print ($number + "\n");
		if ($number == $stoppingNumber)
			break;
	}
}

continue tells the loop to go ahead and start the next loop cycle,
skipping over any code following the continue command.'
Notice that in this case there is no need for an else statement.

The for in loop

Mel has an extremely useful loop called the for-in loop.
It is used for going over all the elements of an array.
I'll demonstrate this first with a normal for loop, then with the for-in loop, so you can see the difference.

{
	string $list[] = {"apples", "butterscotch", "potatoes", "bubblegum", "camels", "peaches"};
	int $size = `size $list`;
	int $i;
	string $current;
	for ($i = 0; $i < $size; $i++)
	{
		$current = $list[$i];
		if ($current == "camels")
		{
			print "I do not eat camels.\n";
		}
	}
}

with a for-in loop here is all you have to do:

{
	string $list[] = {"apples", "butterscotch", "potatoes", "bubblegum", "camels", "peaches"};
	string $current;
	for ($current in $list)
	{
		if ($current == "camels")
		{
			print "I do not eat camels.\n";
		}
	}
}

As you can see, it saves a few lines of code, and it looks neater.
Don't forget about the first way of going over an array.
If you needed to know which element "camels" was in, the standard for loop
would be the way to go.

The do...while loop

Finally there is the do...while loop.
Its syntax reads like this:

{
	int $i = 0;
	do	{
		print ($i + "\n");
		$i++;
		}
	while( $i < 10 );
}

The do...while is just like a while loop, except it executes the code before
checking the condition.
For example:

{
	int $i = 17;
	do	{
		print ($i + "\n");
		$i++;
		}
	while( $i < 10 );
}

In that case $i was already greater than 10, but the code still executed once, because the loop
doesn't check the condition until the end.

I have never really used do..while loops much.  It's always been easy enough
to structure a for or while loop to do the same thing, and their syntax is
a little odd compared to the other loops.
In the end, any one of these loops can perform the same task as any other.
They're designed so that in certain cases, one loop may be more convenient to use than another.
 Use whichever suits your needs, or just use
the one you feel most comfortable with.