Syntax
BD>for : N1=E1,E2,E3 [,N2=E4,E5,E6] ...
BD>...
BD>...Body of commands
BD>...
BD>for : N1=E1,E2,E3 [,N2=E4,E5,E6] ... Command ... Command
where E1, E2, ..., are any valid equations, and N1, N2, ..., are
names, consisting of a sequence of alphanumeric characters beginning
with an alphabetic character.
The FOR: ...END: loops are used to provide loops in
which a control integer varies, and where the control integer is
accessible to the user. We consider two such kinds of loop. The first is
our standard implementation, and the second is a more limited, but much
faster, implementation designed to avoid some of the speed restrictions
that arise from [B/D] being an interpreted language.
For both forms, the intention of the constructs are as follows.
are the names of control variables. The values of
are rounded to the nearest integers, , to give starting, stepping, and stopping values for the
control variables. Thus, the intention of the loop with only the control
variable will be to repeat a block of commands with N1=I1,
N1=I1+I2, and so forth. The stepping value I2 may be negative. In
general, if the stepping value is positive then the loop continues
whilst the control variable has a value not exceeding I3. If the
stepping value is negative, then the loop continues whilst the control
variable has value not less than I3. Furthermore, if the stepping
value is zero; or if it is positive and the starting value exceeds the
stopping value; or if it is negative and the starting value is less than
the stopping value, then all the commands in the loop will be ignored.
For example,
gives an instance of where the loop will be ignored as it is not
possible to reach 2 from one by stepping negatively. On the other hand,
BD>for : i=0, 2, 1, j=-1, 1, 0 ...
establishes a loop which will work through a body of commands with the
control variables taking these values in order: (i=0,j=-1) and
(i=0,j=0). Notice that the stepping value here for i implies the
sequence i=0, i=2, i=4, and so forth, but only the first value is
encountered as the stopping value is i=1.
Notice that entire command lines are parsed as one. Thus, the usage in the
following example
BD>for : i=1,1,2,j=1,2,[i], ...
will fail because at the time of parsing [i] at this point is unknown.
The first form of the syntax is our standard form whereby one line
contains a FOR: statement (and nothing else);
a number of commands on succeeding lines
(which may contain lines with multiple commands); and finally an END:
command to mark the end of the loop. The commands between the
FOR: statement and the END: statement will be repeated
as appropriate, according to the settings of the control variables. An
example of the use of this form of the syntax is as follows.
BD>for : i=1,1,2, j=1,1,2, k=1,1,2
This will print out the following sequence of numbers:
111, 112, 121, 122, 211, 212, 221, 222; showing that the last control
variable encountered in the statement, k, changes fastest.
The second form of the syntax is a special fast form, which we will call
the one-line FOR: statement. It consists of the
FOR: statement, together with one or more commands separated by
the symbol ` ', on the same line. However, the END: command is
not (and may not) be used to mark the end of the loop: the end-of-line
marker does this instead.