Repetitive structure
You may also want to repeat the execution of an instruction or group of instructions, for example if you want to create a series of objects, all slightly different or at different locations.

The for-construct
Suppose you want to create a series of 20 spheres, one partially above the other. To keep track of the number of spheres created, you must declare a variable whose value will change from, for example, 0 to 19. Each time the loop is executed the value of this variable is increased by one. As long as this value remains below 20, the loop is repeated. You can als use this variable's value in the specifation of the position of the new sphere.

int $i;
for ($i = 0; $i < 20; $i++) {
  sphere -pivot 0 $i 0;
}

Explanation:

  • A variable $i is declared to be used as the counter
  • Firstly, the value of $i is initialized to 0
  • [*] Next the condition $i < 20 is checked
    If this condition is fulfilled
  • Then the instruction(s), enclosed within { }, is executed
  • Lastly, the value of $i is increased by one ($i++ is a shorthand for $i = $i + 1)
  • Now, the loop repeats itself starting from the condition [*]

    A helix
    This is the same basic loop, but the position of each sphere also varies in the X and Z-axes, using the sine and cosine function to create a helix shape.

    int $i;
    for ($i = 0; $i < 20; $i++) {
      float $sin = sin($i);
      float $cos = cos($i);
      sphere -pivot $sin $i $cos;
    }

    A spiral
    This is the same basic loop extended to 100 spheres, but the position of each sphere only varies in the X and Z-axes, using $i also as a multiplication factor creating a spiral.

    int $i;
    for ($i = 0; $i < 100; $i++) {
      float $sin = sin($i) * $i / 4;
      float $cos = cos($i) * $i / 4;
      sphere -pivot $cos 0 $sin;
    }

    A spiral surface
    Instead of creating each time a new object from scratch, it is also possible to duplicate the last created object and move it relative to this position. Here a series of circles are created and lofted into a single surface.

    select -all;
    delete; // delete all existing objects
    circle -center 4 0 0;
    int $i;
    for ($i = 0; $i < 60; $i++) {
      duplicate;
      rotate -r 0 30 0;
      move -r 0 .3 0;
    }
    select -all;
    loft;