Repeating statements with initialisation and control¶
for |
Repeat statements with initialisation and control |
SYNTAX
The for construct consists of a three-part statement in brackets, followed by a set of statements in braces. The initialisation and control are themselves statements or sets of statements, while condition is the test that determines if the statements are executed.
The for command firstly performs the initialisation statement(s),and then repeats statements while condition evaluates as true, executing the control statement at the end of each iteration of the loop. The three parts of the for command are contained in brackets and separated by semicolons. Any of the parts may be absent. The semicolons must always be present even if one or more parts are absent.
initialisation |
The initialisation is performed once at the start of the loop, before the first test of condition. It plays no further part. It may consist of a single statement, multiple statements, or be absent altogether. Multiple statements are separated by commas. A semicolon indicates the end of the initialisation statements. If no initialisation statements are required the first semicolon should immediately follow the opening bracket. |
condition |
After each initialisation statement has been performed, condition is tested. If it is false, the loop terminates, and control passes to the statement following the closing brace (}). If condition is true, then the statements are executed. If condition is absent, then it is assumed to be true. An expression that evaluates to zero is false, while one that evaluates to anything else is true. |
control |
The control statement (or statements) is executed at the end of each iteration of the loop. The control may also be a single statement, multiple statements, or be absent. Multiple statements are separated by commas. |
statements |
A block of Sculptor code. The special commands break and continue may be used from within statements to break the loop. The break command causes the loop to terminate and passes control to the statement following the closing brace (}). The special label BREAK can be used on the trap clause of a Sculptor command to perform the same function, e.g.
The continue command causes the current iteration of the loop to end. Statements between the continue command and the closing brace are skipped. However, the control statements at the end of the loop are still executed. The condition is then tested to determine whether the loop continues with a new iteration, or terminates. The special label CONTINUE can be used in the trap clause of a Sculptor command to perform the same function, e.g.
|
NOTES
The opening brace must be on the same line as the for command. If this line is becoming too long, use the continuation character \ to indicate that the line is continued on the following line.
The simple expression 1 may be used as a condition and causes a loop to repeat indefinitely. The pre-defined manifest constant TRUE could also be used. The only way to terminate such a loop is the break command
The statements between the braces may contain other loop or switch constructs (for, switch, while or do).
EXAMPLE
for (Element = 2; Element <=12; Element ++) {
if (Acc.Month[Element] <> "") {
PrintHD = "Month" / " [" + tostr(Element) / "]:"
print fit(PrintHD, 19); tab(DataCol); Acc.Month[Element]
}
}
RELATED TOPICS |