|
| Previous: 2.7.3 Conditional Statements | TOC | Index | Back | Next: 2.7.5 Branch Statements |
There are two kinds of loops: `while' loops and `for' loops which represent two classes of problems: those which are computable by algorithms with a known upper limit of iterations (FOR-computable or primitive recursive functions) and problems which cannot be computed by algorithms with a fixed number of iterations (WHILE-computable or general recursive functions). Since the FOR-computable functions are a subset of the WHILE-computable ones, FOR statements may be considered a special case of WHILE statements and in fact, it is possible to express a FOR loop using WHILE, but not vice versa.
[In most actual programming languages, it is possible to express WHILE using FOR, but theoretically, it is not.]
There is a third kind of loop in many other languages, the repeative loop, but it turns ouf to be a special case of the WHILE loop. Repeating loops are not very frequently needed and if they are, they can easily be constructed using WHILE and IF in T3X.
The WHILE loop has the following general form:
WHILE (expression) statement
where expression may be any expression and statement may be any statement. The body consisting of the statement will be executed while the test expression in parentheses evaluates to some `true' value. Hence the name of this loop. If the expression becomes `false' before the statement has been passed for the first time, the statement will never be executed. However, a loop which tests its exit condition at the end of the statement may be constructed using WHILE, IF, LEAVE, and a compound statement (which will be explained later in this chapter):
WHILE (-1) DO ! -1 = true
statement
IF (\condition) LEAVE;
END
In this case, statement will be executed at least once, because the condition -1 is a `true' constant. In the subsequent IF statement, the loop will be left if condition does not apply. LEAVE is used to branch out of a loop. Details will be explained later.
The FOR loop exists in two forms: an explicit form and a short form. The explicit form looks as follows:
FOR (var=start, limit, step) statement
Var is an atomic variable which must have been declared earlier in the program. Unlike in BCPL, it will not be declared implicitly by the FOR statement. Start and limit are expressions and step is a constant expression. The FOR loop works this way:
First, var is initialized with the value of start.
Second, var is compared against limit. If either the condition
var<limit /\ step>=0
or
var>limit /\ step<0
holds, the statement is executed. Otherwise the loop is left and statement will not be executed any more.
Finally, step is added to var, and the loop will be repeated from the point where the exit condition is checked. Like in a WHILE loop, the statement will never be executed, if the exit condition already is true when it is checked for the first time (which is the case, if neither of the above conditions holds).
The following examples print the numbers from 0 to 9 using the procedure print (which is only defined in the first example). (Print uses some routines of the classes t3x and string which will be explained in a later chapter.)
MODULE count(t3x, string);
OBJECT t[t3x], str[string];
print(n) DO VAR b::10;
t.write(T3X.SYSOUT, str.format(b, "%D\n", [(n)]),
str.length(b));
END
DO VAR i;
FOR (i=0, 10, 1) print(i);
END
This loop would count down from 9 to 0:
DO VAR i;
FOR (i=9, -1, -1) print(i);
END
Special attention should be paid to the limits of the FOR loops in these examples. They always specify the first value which will not be applied to the statement. Another way to write the second example would be the following one, where the FOR loop has been replaced by a WHILE loop:
DO VAR i;
i := 9;
WHILE (i > -1) DO
print(i);
i := i-1;
END
END
The meaning of this program fragment is completely equal to the one employing a FOR loop, but the syntax of the FOR statement is more compact and expresses the purpose of the statement clearer.
The step value is optional in FOR statements. In the short form of the statement, it is omitted. If only two operands are specified in FOR, the step width defaults to one. Therefore, the statements
FOR (j=0, 100, 1) p(i);
and
FOR (j=0, 100) p(i);
have exactly the same meaning.
| Previous: 2.7.3 Conditional Statements | TOC | Index | Back | Next: 2.7.5 Branch Statements |