next up previous contents
Next: 3.6 Conditional statements Up: 3. FORTRAN style interpreter Previous: 3.4 Intrinsic functions

   
3.5 Loops

Loops can be programmed in DISCUS using the 'do' command. Three different types of loops are implemented. The first type executes a predefined number of times. The syntax for this type of loop is

do <variable> = <start>,<end> [,<increment>]
...commands to be executed ...
enddo

Loops may contain constants or arithmetic expressions for <start>, <end>, and <increment>. <increment> defaults to 1. The internal type of the variables is real. The loop counter is evaluated from (<end> - <start>)/<increment> + 1. If this is negative, the loop is not executed at all. The parameters for the counter variable, start end and increment variables are evaluated only at the beginning of the do - loop and stored in internal variables. It is possible to change the values of <variable>, <start> and/or <end> within the loop without any effect on the performance of the loop. This practice is not encouraged, could, however, be an unexpected source of errors.

The second type of loop is executed while <logical expression> is true. Thus it might not be executed at all. The syntax for this type of loop is

do while <logical expression>
...commands to be executed ...
enddo

The last type of loop is executed until <logical expression> is true. This loop, however, is always executed once and has the following syntax

do
...commands to be executed ...
enddo until
<logical expression>

In the body of commands any valid DISCUS command can be used. This includes calls to the sublevels, further do loops or macros, even if these macros contain do loops themselves. The maximum level of nesting is limited by the parameter MAXLEV in the file 'doloop.inc'. If necessary adjust this parameter to allow for deeper nesting. All commands from the first 'do' command to the corresponding 'enddo' are read and stored in an internal array. This array can take at most MAXCOM (defined in file 'doloop.inc' as well) commands at every level of nesting. If lengthy macro files are included in the do loop, this parameter might have to be adjusted.

If a do loop (or an if block) needs to be terminated, the 'break' command will perform this function. The parameter on the 'break' command line gives the number of nested levels of 'do' and 'if' blocks to be terminated. The interpreter will continue execution with the first command following the corresponding 'enddo' or 'endif' command. An example is given below, note, that the line numbers are only given for better orientation and are no actual part of the listed commands.

     1  do i[2]=1,5
     2    do i[1]=1,5
     3      if ((i[1]+i[2]) .eq 6) then
     4        break 2
     5      endif
     6    enddo
     7  enddo

In this example, the execution of the inner do-loop will stop as soon as the sum of the two increment variables i[1] and i[2] is equal to 6. The program continues with the 'enddo' line of the outer do - loop. Notice that two levels need to be interrupted, the if block and the innermost do loop. If the parameter had bin equal to one, only the if block would have been interrupted, while the innermost do loop would have continued without break.


next up previous contents
Next: 3.6 Conditional statements Up: 3. FORTRAN style interpreter Previous: 3.4 Intrinsic functions
Thomas Proffen {Billinge}
1999-03-04