POV-Ray : Documentation : 1.3.9.7 Mixed-type nested loops
 POV-Ray 3.6 Documentation Online View

#### 1.3.9.7 Mixed-type nested loops

It is perfectly possible to put a for-loop inside a non-for-loop or vice-versa. Again, you just have to be careful (with experience it gets easier).

For example, suppose that we want to create 50 spheres which are randomly placed inside the unit sphere.

So the distinction is clear: First we need a loop to create 50 spheres (a for-loop type suffices) and then we need another loop inside it to calculate the location of the sphere. It will look like this:

```#declare S = seed(0);
#declare Index = 1;
#while(Index <= 50)

#declare Point = <2*rand(S)-1, 2*rand(S)-1, 2*rand(S)-1>;
#while(vlength(Point) > 1)
#declare Point = <2*rand(S)-1, 2*rand(S)-1, 2*rand(S)-1>;
#end

sphere { Point, .1 }

#declare Index = Index + 1;
#end
```

There are some important things to note in this specific example:

• Although this is a nested loop, the sphere is not created in the inner loop but in the outer one. The reason is clear: We want to create 50 spheres, so the sphere creation has to be inside the loop that counts to 50. The inner loop just calculates an appropriate location.
• The seed value 'S' is declared outside all the loops although it is used only in the inner loop. Can you guess why? (Putting it inside the outer loop would have caused an undesired result: Which one?)