Controls a sequence of repetitions. A basic <b>for</b> structure has three parts: <b>init</b>, <b>test</b>, and <b>update</b>. Each part must be separated by a semicolon (;). The loop continues until the <b>test</b> evaluates to <b>false</b>. When a <b>for</b> structure is executed, the following sequence of events occurs:<br />
<br />
1. The init statement is run.<br />
2. The test is evaluated to be true or false.<br />
3. If the test is <em>true</em>, jump to step 4. If the test is <em>false</em>, jump to step 6.<br />
4. Run the statements within the block.<br />
5. Run the update statement and jump to step 2.<br />
6. Exit the loop.<br />
<br />
In the first example above, the <b>for</b> structure is executed 40 times. In the init statement, the value <em>i</em> is created and set to zero. <em>i</em> is less than 40, so the test evaluates as <em>true</em>. At the end of each loop, <em>i</em> is incremented by one. On the 41st execution, the test is evaluated as <em>false</em>, because <em>i</em> is then equal to 40, so <em>i < 40</em> is no longer true. Thus, the loop exits.<br />
<br />
A second type of <b>for</b> structure makes it easier to iterate over each element of an array. The last example above shows how it works. Within the parentheses, first define the datatype of the array, then define a variable name. This variable name will be assigned to each element of the array in turn as the <b>for</b> moves through the entire array. Finally, after the colon, define the array name to be used.
]]></description>
<syntax>
for (<c>init</c>; <c>test</c>; <c>update</c>) {
<c>statements</c>
}
for (<c>datatype</c> <c>element</c> : <c>array</c>) {
<c>statements</c>
}
</syntax>
<parameter>
<label>init</label>
<description><![CDATA[statement executed once when beginning loop]]></description>
</parameter>
<parameter>
<label>test</label>
<description><![CDATA[if the test evaluates to <em>true</em>, the statements execute]]></description>
</parameter>
<parameter>
<label>update</label>
<description><![CDATA[executes at the end of each iteration]]></description>
</parameter>
<parameter>
<label>statements</label>
<description><![CDATA[collection of statements executed each time through the loop]]></description>
</parameter>
<parameter>
<label>datatype</label>
<description><![CDATA[datatype of elements in the array]]></description>
</parameter>
<parameter>
<label>element</label>
<description><![CDATA[temporary name to use for each element of the array]]></description>
</parameter>
<parameter>
<label>array</label>
<description><![CDATA[name of the array to iterate through]]></description>