<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.povray.org/content?action=history&amp;feed=atom&amp;title=HowTo%3AUse_for_loop_over_traditional_while_loop</id>
	<title>HowTo:Use for loop over traditional while loop - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.povray.org/content?action=history&amp;feed=atom&amp;title=HowTo%3AUse_for_loop_over_traditional_while_loop"/>
	<link rel="alternate" type="text/html" href="https://wiki.povray.org/content?title=HowTo:Use_for_loop_over_traditional_while_loop&amp;action=history"/>
	<updated>2026-04-03T21:39:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>https://wiki.povray.org/content?title=HowTo:Use_for_loop_over_traditional_while_loop&amp;diff=8869&amp;oldid=prev</id>
		<title>Wfpokorny: Adding content for howto for loop over while loop.</title>
		<link rel="alternate" type="text/html" href="https://wiki.povray.org/content?title=HowTo:Use_for_loop_over_traditional_while_loop&amp;diff=8869&amp;oldid=prev"/>
		<updated>2016-11-23T13:27:53Z</updated>

		<summary type="html">&lt;p&gt;Adding content for howto for loop over while loop.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
===Loops===&lt;br /&gt;
Traditionally in POV-Ray the &amp;lt;code&amp;gt;#while&amp;lt;/code&amp;gt; directive has been the way to implement loops. In 3.7 a new &amp;lt;code&amp;gt;#for&amp;lt;/code&amp;gt; directive was added that makes coding counting loops much simpler.&lt;br /&gt;
&lt;br /&gt;
===Nested Loops===&lt;br /&gt;
Loops can of course be nested. This capability is often used to lay out very large numbers of objects using an economical amount of code.&lt;br /&gt;
&lt;br /&gt;
First, here's a very simple, complete scene file containing a nested &amp;lt;code&amp;gt;#while&amp;lt;/code&amp;gt; loop with which you can play:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=pov&amp;gt;&lt;br /&gt;
#include &amp;quot;colors.inc&amp;quot;&lt;br /&gt;
#include &amp;quot;woods.inc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
camera {location &amp;lt;-1.5,0.9,-0.08&amp;gt; look_at &amp;lt;0,0.7,0.5&amp;gt;}  &lt;br /&gt;
light_source {&amp;lt;-20,75,-100&amp;gt; White}&lt;br /&gt;
background {LightBlue}&lt;br /&gt;
plane {y,0 pigment {color GreenYellow}&lt;br /&gt;
&lt;br /&gt;
#declare RandomSeed = seed(1);&lt;br /&gt;
#declare XCount=0;&lt;br /&gt;
#while (XCount &amp;lt; 200)&lt;br /&gt;
  #declare ZCount=0;&lt;br /&gt;
  #while (ZCount &amp;lt; 200)&lt;br /&gt;
    box {&amp;lt;-0.05,0,-0.05&amp;gt;&amp;lt;0.05,1,0.05&amp;gt;&lt;br /&gt;
      rotate &amp;lt;3*rand(RandomSeed),5*rand(RandomSeed),3*rand(RandomSeed)&amp;gt;&lt;br /&gt;
      texture {T_Wood1 translate &amp;lt;rand(RandomSeed),ZCount,rand(RandomSeed)&amp;gt; rotate x*90 scale 0.07}&lt;br /&gt;
      translate &amp;lt;XCount,0,ZCount&amp;gt; &lt;br /&gt;
    }&lt;br /&gt;
    #declare ZCount=ZCount+1;&lt;br /&gt;
  #end &lt;br /&gt;
  #declare XCount=XCount+1;&lt;br /&gt;
#end &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's the same looping accomplished with the new &amp;lt;code&amp;gt;#for&amp;lt;/code&amp;gt; loop directive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=pov&amp;gt;&lt;br /&gt;
#include &amp;quot;colors.inc&amp;quot;&lt;br /&gt;
#include &amp;quot;woods.inc&amp;quot;&lt;br /&gt;
&lt;br /&gt;
camera {location &amp;lt;-1.5,0.9,-0.08&amp;gt; look_at &amp;lt;0,0.7,0.5&amp;gt;}  &lt;br /&gt;
light_source {&amp;lt;-20,75,-100&amp;gt; White}&lt;br /&gt;
background {LightBlue}&lt;br /&gt;
plane {y,0 pigment {color GreenYellow}&lt;br /&gt;
&lt;br /&gt;
#declare RandomSeed = seed(1);&lt;br /&gt;
#for (XCount,0,199)&lt;br /&gt;
  #for (ZCount,0,199)&lt;br /&gt;
    box {&amp;lt;-0.05,0,-0.05&amp;gt;&amp;lt;0.05,1,0.05&amp;gt;&lt;br /&gt;
      rotate &amp;lt;3*rand(RandomSeed),5*rand(RandomSeed),3*rand(RandomSeed)&amp;gt;&lt;br /&gt;
      texture {T_Wood1 translate &amp;lt;rand(RandomSeed),ZCount,rand(RandomSeed)&amp;gt; rotate x*90 scale 0.07}&lt;br /&gt;
      translate &amp;lt;XCount,0,ZCount&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
  #end&lt;br /&gt;
#end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These example uses 2 nested loops. Each nested loop has its own control variable. It starts with both XCount and YCount equal to zero. With XCount at zero, YCount loops from 0 to 199, then XCount is incremented to 1, YCount is reset to zero and the second loop repeats.&lt;br /&gt;
&lt;br /&gt;
This example also introduces a degree of randomness in each iteration using the POV-Ray seed and rand functions. When generating a large number of objects using loops you'll find that a little randomness goes a long way. This example generates 40,000 wooden posts, each with a little bit of a wobble and each with a slightly different wood texture pattern. It should take about 10 or 20 seconds to render&lt;/div&gt;</summary>
		<author><name>Wfpokorny</name></author>
	</entry>
</feed>