next up previous contents index
Next: General remarks Up: Commands controlling program flow Previous: Commands controlling program flow

Looping with a control variable

   


tex2html_wrap_inline33790 tex2html_wrap_inline33790 Syntax

  1. BD>for : N1=E1,E2,E3 [,N2=E4,E5,E6] ... tex2html_wrap_inline33712

    BD>... tex2html_wrap_inline33712

    BD>...Body of commands tex2html_wrap_inline33712

    BD>... tex2html_wrap_inline33712

    BD>end : tex2html_wrap_inline33712

  2. BD>for : N1=E1,E2,E3 [,N2=E4,E5,E6] ... tex2html_wrap_inline33876 Command tex2html_wrap_inline33876 ... tex2html_wrap_inline33876 Command tex2html_wrap_inline33712

where E1, E2, ..., are any valid equations, and N1, N2, ..., are names, consisting of a sequence of alphanumeric characters beginning with an alphabetic character.

tex2html_wrap_inline33806 tex2html_wrap_inline33806

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. tex2html_wrap_inline34010 are the names of control variables. The values of tex2html_wrap_inline34012 are rounded to the nearest integers, tex2html_wrap_inline34014 , to give starting, stepping, and stopping values for the control variables. Thus, the intention of the loop with only the control variable tex2html_wrap_inline34016 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,

BD>for : i=1,-1,2 ... tex2html_wrap_inline33712

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 ... tex2html_wrap_inline33712

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], ... tex2html_wrap_inline33712

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 tex2html_wrap_inline33712

BD>print : [i][j][k] tex2html_wrap_inline33712

BD>end : tex2html_wrap_inline33712

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 ` tex2html_wrap_inline33876 ', 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.




next up previous contents index
Next: General remarks Up: Commands controlling program flow Previous: Commands controlling program flow

David Wooff
Wed Oct 21 15:14:31 BST 1998