Mel Script - the for loop

 Intro A for loop is used within programming whenever we need to repeat something a known number of times. If you know exactly the number of times you need to repeat an action, you should use a for loop. If you don't know when the repetition will finish, then look at using a for loop. If you need to specifically do something to the contents of an array, then you might want to use a for-in loop. The general form for a for loop is as follows :

 ``` for( ; ; ) { // action to repeat } ```

 Example 1 : A basic for loop example A for loop is used to create 10 spheres ``` { // repeat the action ten times. \$i is a variable we will use // to count the number of times that we repeat, and therefore // when to end the repetition. Unlike C and other languages, // we do not have to specify a type for \$i, Maya guesses that // based on what value we first assign it. Since in this case // \$i is assigned zero, Maya assumes it's an integer... // // \$i starts at zero, // \$i must be less than 10 for the repetition to continue, // \$i will be incremented by 1 each time we loop // for( \$i=0; \$i<10; ++\$i ) { // create a sphere. The new sphere will be the currently // selected object. sphere; // move the selected object a bit in X xform -translation (2*\$i) 0 0; } } ```

 Example 2 : Using Comma ``` { // repeat the action ten times. \$i is a variable we will use // to count the number of times that we repeat, and therefore // when to end the repetition. Unlike C and other languages, // we do not have to specify a type for \$i, Maya guesses that // based on what value we first assign it. Since in this case // \$i is assigned zero, Maya assumes it's an integer... // // \$i starts at zero, // \$i must be less than 10 for the repetition to continue, // \$i will be incremented by 1 each time we loop // for( \$i=0,\$j=0.0; \$i<10; ++\$i,\$j+=0.75 ) { // create a sphere. The new sphere will be the currently // selected object. sphere; // move the selected object a bit in X xform -translation (2*\$i) 0 0; // create a sphere. The new sphere will be the currently // selected object. polyCube ; // move the selected object a bit in X xform -translation (2*\$j) 2 0; } } ```