73- Loops and IFs

Embed Size (px)

Citation preview

  • 8/10/2019 73- Loops and IFs

    1/2

    Loops and IFs'

    Two elements which must exist before a language can be useful are some means of altering executionand 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 thecommands which follow will be executed until the &ELSEis encountered. If LPHRASE(1)

    is .FALSE., then MOSES will evaluate LPHRASE(2). If it is .TRUE., then the commands following&ELSEIFwill be executed until &ELSEis encountered, otherwise the commands between &ELSE,and &ENDIFwill be executed. Both &ELSEIFand &ELSEmay be omitted, and more than one

    &ELSEIFcan be placed between an &IFand &ELSE, but &IFand &ENDIFmust always bepresent. There is virtually no limit on the nesting of&IFblocks.

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

    &LOOP, I NDEX, BEGVAL, ENDVAL, I NCR

    &LOOP, VAR, ( LI ST(1) , . . . . . . LI ST(n) )

    &NEXT, LPHRASE

    &EXIT, LPHRASE

    &ENDLOOP

    The &LOOPcommand marks the beginning of a set of commands which will be repeated and the&ENDLOOPcommand 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 oneof two ways depending upon the form of the &LOOPcommand itself. The parameters on the first

    form of the &LOOPcommand allow for an "indexed" loop. In other words, INDEX is the name of alocal variable which will be set to the integerBEGVAL at the beginning of the loop, and it will beupdated 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 &LOOPis a "while" on the values inparenthesis. In other words, with this form, the variable VAR will be set to the value LIST(1) for thefirst 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 &EXITcommand is encountered with alogical phrase of .TRUE.. The commands between the &LOOPand the &ENDLOOPare executed

    in order. A "jump" to the bottom of a loop occurs whenever an &NEXTcommand is encounteredwith 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 %

    Page 1 of 2

  • 8/10/2019 73- Loops and IFs

    2/2

    &NEXT &LOGI CAL( %J % . EQ. 9)&EXI T &LOGI CAL( %I % . GT. 11)&TYPE Thi s i s 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 istyped 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 willterminate execution. Similarly, one can conditionally terminate execution of the program by:

    &FINISH, LPHRASE

    which terminates the program if LPHRASE is .TRUE..

    Page 2 of 2