Loops and IFs' - Ultramarine.com Loops and IFs'

Two elements which must exist before a language can be useful are some means of altering execution and some method of repetition. Here, the execution control is offered by a standard block IF construct:


     &IF, LPHRASE(1), &THEN

     &ELSEIF, LPHRASE(2), &THEN

     &ELSE

     &ENDIF

Here, LPHRASE(1) and LPHRASE(2) are "logical phrases", and if LPHRASE(1) is .TRUE., then the commands which follow will be executed until the &ELSE is encountered. If LPHRASE(1) is .FALSE., then MOSES will evaluate LPHRASE(2). If it is .TRUE., then the commands following &ELSEIF will be executed until &ELSE is encountered, otherwise the commands between &ELSE, and &ENDIF will be executed. Both &ELSEIF and &ELSE may be omitted, and more than one &ELSEIF can be placed between an &IF and &ELSE, but &IF and &ENDIF must always be present. There is virtually no limit on the nesting of &IF blocks.

In MOSES, the concept of repetition is implemented via the constructs:


     &LOOP, INDEX, BEGVAL, ENDVAL, INCR

     &LOOP, VAR, ( LIST(1), ...... LIST(n) )

     &NEXT, LPHRASE

     &EXIT, LPHRASE

     &ENDLOOP

The &LOOP command marks the beginning of a set of commands which will be repeated and the &ENDLOOP command marks the end. When this construct is encountered, MOSES will continue to execute the commands delimited until a termination criteria is satisfied. Termination can occur in one of two ways depending upon the form of the &LOOP command itself. The parameters on the first form of the &LOOP command allow for an "indexed" loop. In other words, INDEX is the name of a local variable which will be set to the integer BEGVAL at the beginning of the loop, and it will be updated to its current value plus INCR at each repetition. Loops of this type will terminate when INDEX is greater than ENDVAL. The second form of &LOOP is a "while" on the values in parenthesis. In other words, with this form, the variable VAR will be set to the value LIST(1) for the first trip through the loop, LIST(2) for the second trip, etc. The loop will terminate after LIST(n) has been used. Alternately, a loop will terminate whenever an &EXIT command is encountered with a logical phrase of .TRUE.. The commands between the &LOOP and the &ENDLOOP are executed in order. A "jump" to the bottom of a loop occurs whenever an &NEXT command is encountered with a logical phrase of .TRUE.. An example of the first form of the loop command is shown below:

     &LOOP I 1 &TOKEN(N %NODES%) 1
     &SET J = %I%
     &NEXT &LOGICAL(%J% .EQ. 9)
     &EXIT &LOGICAL(%I% .GT. 11)
     &TYPE This is Loop Number %I%
     &ENDLOOP

Here, I is the INDEX, and the loop continues from 1 to the number of tokens in the variable NODES, in increments of 1. The variable J is set to the value of I each pass through the loop, and a message is typed to the screen. If the J is equal to 9, the message is not printed. If I is greater than 11, or if I is the number of tokens in the variable NODES, the loop is terminated.

To terminate execution of either a macro or a command file, one can use the command:


     &EOFILE, LPHRASE

When this command is encountered, and LPHRASE is .TRUE., the command file or macro will terminate execution. Similarly, one can conditionally terminate execution of the program by:


     &FINISH, LPHRASE

which terminates the program if LPHRASE is .TRUE..