Descriptions and Examples for the POV-Ray Raytracer by Friedrich A. Lohmüller
About Loops with POV-Ray     
Italiano Italiano
Français français
Deutsch Deutsch

Home
- POV-Ray Tutorial

  Loops in POV-Ray
>1. For + While
       Comparison
  2. Linear
      Transformations
  3. Circular
      Transformations
  4. Moebius etc.
  5. Screws
       & Spirals
  6. Twisting
       Spirals
  7. Snails
       & Ammonites
  8. Spherical Spirals 1
  9. Spherical Spirals 2
 10. Fibonacci Spirals
                                       
 
 
  - Download

For Loops and While Loops - Comparison

The for loop is implemented in POV-Ray 3.7 (since beta 37).
(Attention: The for loop don't work with version 3.6.2 !)
Here a comparison of these two similar kind of loops:

To avoid any collision with built-in identifiers and reserved words in POV-Ray,
it's strongly recommanded to use only words beginning with capital letters
for all identifiers of variables declared by the user.

We have to take into account that x = <1,0,0> is a built-in identifier, we have to use i.e. "X" instead.

Syntax 'While Loop':

//------------------------------------
#local Identifier = Start;
#while (Identifier <= End)
  //... do something
  // depending on value of Identifier.
  #local Identifier = Identifier+Step;
#end // ------------- end of loop ----

Syntax 'For Loop':

//----------------------------------
#for (Identifier, Start, End [, Step])
  //... do something
  // depending on value of Identifier.
#end // ------------- end of loop --
Note: Step is optional! (default Step=1)

Example 'While Loop':

union{ //-----------------------------
 #local Cntr = 0; // start
 // while loop
 #while ( Cntr <= 5 )

   object{ MyObject
           translate<0,0, Cntr*2 >
         } // end of object

 #local Cntr = Cntr + 1; // next
 #end // -------- end of #while loop

 rotate<0,0,0>
 translate<0,0,0>
} // end of union --------------------

Example 'For Loop':

union{ //-----------------------------

//#for (Identifier, Start, End [, Step])
 #for (Cntr, 0, 5, 1)

   object{ MyObject
           translate<0,0, Cntr*2 >
         } // end of object

 #end // ----------- end of #for loop

 rotate<0,0,0>
 translate<0,0,0>
} // end of union --------------------
Note: 5 steps = 6 footprints!

For the declaration of 'MyObject' see the scene file text!
Scene file for POV-Ray: .pov or: .txt

The length of the steps can be any real number value! Als negative values are allowed.
The above loops can also be done in this way:
while loop:

 #local Cntr = 0; // start
 #while ( Cntr <= 10 )
   object{ MyObject
           translate<0,0, Cntr >
         } // end of object
 #local Cntr = Cntr + 2; // next
 #end // -------- end of #while loop
for loop:

 #for (Cntr, 0, 10, 2)//..,Start,End,Step
   object{ MyObject
           translate<0,0, Cntr >
         } // end of object
 #end // ----------- end of #for loop

Warning:   Avoiding infinite loops is at your own risk!
Make sure that your loop really stops at a finite number of steps!
Otherwise POV-Ray will parse an endless loop by filling your RAM with useless data!
See the following example of a loop which never ends (at least until you don't hit the red 'Stop' button):
A while loop with a wrong step size:

#local Cntr = 0;  // start
#local End  = 5;  // end
#local Step = - 1; // step !!wrong!!
 // while loop start
 #while ( Cntr <= End )
   object{ MyObject
           translate<0,0,Cntr*2>}
 #local Cntr = Cntr + Step; // next
 #end // -------- end of while loop
This while loop never stops!
 
 
 
 
 
With a for loop this is not so dramatic:

 #for (Cntr, 0, 5, -1)
    // do something
 #end // ------ end of #for loop
This 'nonsense for loop' results in just doing nothing!

top

© Friedrich A. Lohmüller, 2014
http://www.f-lohmueller.de