<for>

Purpose The <for> statement is used to loop repeatedly until a certain limit is reached. At each iteration of the loop some Superx++ statements can be executed.
Format <for>
   <init>
      {initial statements}
   </init>
   <cond>
      {loop condition}
   </cond>
   <step>
      {step statements}
   </step>
   {iteration statements}
</for>
{initial statements} the statements to be executed before the first iteration of the loop.
{loop condition} the condition to be evaluated: if true then the loop will continue; if false then the loop will terminate.
{step statements} the statements to be executed after each iteration of the loop.
{iteration statements} the statements to be executed with each iteration of the loop before the {step statements}.
Example #1 <class name="XEmployee" inherit="">
   <construct />
   <scope type="public">
      <var name="MyCount" type="int" />
   </scope>
</class>
<node name="MyEmp" class="XEmployee" />
<for>
   <init>
      <eval object="MyEmp" member="MyCount">0</eval>
   </init>
   <cond>
      <eval>
         <parm type="int" name="a"><eval object="MyEmp" member="MyCount" /></parm>
         <parm type="int" name="b">10</parm>
         <expr>a &lt; b</expr>
      </eval>
   </cond>
   <step>
      <eval object="MyEmp" member="MyCount">
         <eval>
            <parm type="int" name="b"><eval object="MyEmp" member="MyCount" /></parm>
            <expr>b + 1</expr>
         </eval>
      </eval>
   </step>
   <xout>\r\nFor\tinstance = </xout>
   <xout><eval object="MyEmp" member="MyCount" /></xout>
</for>

The loop starts by executing the <init> clause. Then the <cond> clause executes to evaluate whether or not the loop should continue or terminate and pass control to the next statement in the program. If the <cond> clause evaluated to true then the statements found after the <step> clause are executed. After they have executed the <step> clause executes and then the <cond> clause is re-executed and the cycle continues. The result of the code above is that the following text is sent to the output stream:

For instance = 0
For instance = 1
For instance = 2
For instance = 3
For instance = 4
For instance = 5
For instance = 6
For instance = 7
For instance = 8
For instance = 9

Example #2 <class name="XEmployee" inherit="">
   <construct />
   <scope type="public">
      <var name="MyCount" type="int" />
   </scope>
</class>
<node name="MyEmp" class="XEmployee" />
<for>
   <init>
      <eval object="MyEmp" member="MyCount">0</eval>
   </init>
   <cond>
      <eval>
         <parm type="int" name="a"><eval object="MyEmp" member="MyCount" /></parm>
         <parm type="int" name="b">10</parm>
         <expr>a &lt; b</expr>
      </eval>
   </cond>
   <step>
      <eval object="MyEmp" member="MyCount">
         <eval>
            <parm type="int" name="b"><eval object="MyEmp" member="MyCount" /></parm>
            <expr>b + 1</expr>
         </eval>
      </eval>
   </step>
   <xout>\r\nFor\tinstance = </xout>
   <xout><eval object="MyEmp" member="MyCount" /></xout>
   <if>
      <cond>
         <eval>
            <parm type="int" name="a">5</parm>
            <parm type="int" name="b"><eval object="MyEmp" member="MyCount" /></parm>
            <expr>a = b</expr>
         </eval>
      </cond>
      <true>
         <xout>\r\nBreak: If\tinstance = </xout>
         <xout><eval object="MyEmp" member="MyCount" /></xout>
         <break />
      </true>
      <false />
   </if>
</for>

The loop starts by executing the <init> clause. Then the <cond> clause executes to evaluate whether or not the loop should continue or terminate and pass control to the next statement in the program. If the <cond> clause evaluated to true then the statements found after the <step> clause are executed. After they have executed the <step> clause executes and then the <cond> clause is re-executed and the cycle continues. Things would be exactly the same as in the previous example but the <break /> statement prematurely terminates the loop. The <break> statement is a special instruction which forcibly terminates any loop that it is placed within. The loop does not have to be created by a <for> statement but can be caused by any of the loop statements: i.e. <for>, <do> ... <cond>, <do> ... <until> or <while>. In the case of nested loops, if the <break> statement is in the outer loop then the <break> statement will terminate the loop that it is in and execution will continue within the outer loop. The result of the code above is that the following text is sent to the output stream:

For instance = 0
For instance = 1
For instance = 2
For instance = 3
For instance = 4
For instance = 5
Break: If instance = 5