Programmer Guide/Command Reference/FOR: Difference between revisions

From STX Wiki
Jump to navigationJump to search
Line 1: Line 1:
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
==FOR==
The <code>FOR</code> executes the commands in the block enclosed by the <code>FOR</code> statement and the corresponding <code>END</code> as long as the (slightly misleadingly named) <code>TO</code> condition holds true.
 
FOR [ <var>target</var> := <var>init</var> ] TO <var>cond</var> STEP <var>step</var>
<code>FOR [ <var>target</var> := <var>init</var> ] TO <var>cond</var> STEP <var>step</var> // commands
    // commands
END</code>
    // ...
 
END
Excecute the commands in the FOR loop while the condition holds true.
 
;<var>target</var>
;<var>target</var>
:The incremental counter variable to initialize with the value <var>init</var> (e.g. #i).
:The incremental counter variable to initialize with the value <var>init</var> (e.g. #i).


;<var>init</var>
;<var>init</var>
:The value with which to initialize the counter variable <var>target</var> (e.g. 0).
:The value with which to initialize the counter variable <var>target</var> (e.g. 0).


;<var>cond</var>
;<var>cond</var>
:The condition which should be tested at the start of each loop (e.g. $#i < 10). See the [[Programmer Guide/Command Reference Intro/condition|condition]] topic for the condition syntax.
:The condition which should be tested at the start of each loop (e.g. $#i < 10). See the [[Programmer Guide/Command Reference Intro/condition|condition]] topic for the condition syntax.


;<var>step</var>
;<var>step</var>
:The incremental value to add to the counter variable <var>target</var> at the end of each loop. The assignment syntax (e.g. #i := int $#i+1) is allowed.
:The incremental value to add to the counter variable <var>target</var> at the end of each loop. The assignment syntax (e.g. #i := int $#i+1) is allowed.
FOR #i := 0 TO $#i < 10 STEP #i := int $#i+1
    BUTIL MSGBOX MSG; #i = $#i
END


<pre>
// an example without initialisation
FOR #i := 0 TO $#i < 10 STEP #i := int $#i+1
FOR TO $#x > $#y STEP #i := int $#i + 1
        BUTIL MSGBOX MSG; #i = $#i
    // do something here ...
END
END
// an example without initialisation
FOR TO $#x > $#y STEP #i := int $#i + 1
  // do something here ...
END
</pre>

Revision as of 21:26, 22 March 2011

The FOR executes the commands in the block enclosed by the FOR statement and the corresponding END as long as the (slightly misleadingly named) TO condition holds true.

FOR [ target := init ] TO cond STEP step
   // commands
   // ...
END
target
The incremental counter variable to initialize with the value init (e.g. #i).
init
The value with which to initialize the counter variable target (e.g. 0).
cond
The condition which should be tested at the start of each loop (e.g. $#i < 10). See the condition topic for the condition syntax.
step
The incremental value to add to the counter variable target at the end of each loop. The assignment syntax (e.g. #i := int $#i+1) is allowed.
FOR #i := 0 TO $#i < 10 STEP #i := int $#i+1
    BUTIL MSGBOX MSG; #i = $#i
END
// an example without initialisation
FOR TO $#x > $#y STEP #i := int $#i + 1
   // do something here ...
END

Navigation menu

Personal tools