Programmer Guide/Command Reference/FOR: Difference between revisions
From STX Wiki
Jump to navigationJump to search
m (Text replace - "Command_Reference_Intro" to "Concepts") |
No edit summary |
||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:{{SUBPAGENAME}}}} | {{DISPLAYTITLE:{{SUBPAGENAME}}}} | ||
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. | The <code>FOR</code> loop 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 [[Programmer_Guide/Concepts/Conditional_Expressions|<var>condition</var>]] STEP <var>step</var> | FOR [ <var>target</var> := <var>init</var> ] TO [[Programmer_Guide/Concepts/Conditional_Expressions|<var>condition</var>]] STEP <var>step</var> | ||
// commands | // commands | ||
// | // … | ||
END | END | ||
;<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. <var>#i</var>). | ||
;<var>init</var> | ;<var>init</var> | ||
Line 17: | Line 17: | ||
:The (slightly misleadingly named) <code>step</code> clause is an {{STX}} statement for incrementing the counter variable <var>target</var> at the end of each loop, e.g. the statement <code>#i := int $#i+1</code>. | :The (slightly misleadingly named) <code>step</code> clause is an {{STX}} statement for incrementing the counter variable <var>target</var> at the end of each loop, e.g. the statement <code>#i := int $#i+1</code>. | ||
FOR #i := 0 TO $#i < 10 STEP #i := int $#i+1 | FOR #i := 0 TO $#i < 10 STEP #i := int $#i+1 | ||
BUTIL MSGBOX MSG; #i | BUTIL MSGBOX MSG; Variable #i currently is $#i | ||
END | END | ||
// an example without initialisation | // an example without initialisation | ||
FOR TO $#x > $#y STEP #i := int $#i + 1 | FOR TO $#x > $#y STEP #i := int $#i + 1 | ||
// do something here | // do something here … | ||
END | END | ||
Neither of <var>target</var>, <var>init</var>, <var>condition</var> and <var>step</var> should ever contain one of the strings "<code>FOR</code>", "<code>TO</code>" or "<code>STEP</code>". If it does, the amazing {{STx}} will most likely mistake the line for an ill-formed <code>FOR</code> statement. |
Revision as of 15:28, 25 April 2014
The FOR
loop 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 condition 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).
- condition
- The condition which should be tested at the start of each loop (e.g.
$#i < 10
). See Conditional Expressions for further information on conditional expressions.
- step
- The (slightly misleadingly named)
step
clause is an STx statement for incrementing the counter variable target at the end of each loop, e.g. the statement#i := int $#i+1
.
FOR #i := 0 TO $#i < 10 STEP #i := int $#i+1 BUTIL MSGBOX MSG; Variable #i currently is $#i END
// an example without initialisation FOR TO $#x > $#y STEP #i := int $#i + 1 // do something here … END
Neither of target, init, condition and step should ever contain one of the strings "FOR
", "TO
" or "STEP
". If it does, the amazing STx will most likely mistake the line for an ill-formed FOR
statement.