next up previous
Next: Examples of Loop Counts Up: Control Flow Previous: Named and Nested Loops

 

Indexed DO Loop

Loops can be written which cycle a fixed number of times.

The syntax is as follows,

 
DO  tex2html_wrap_inline21419  DO-var  tex2html_wrap_inline21421 = tex2html_wrap_inline21419  expr1  tex2html_wrap_inline21421 , tex2html_wrap_inline21419  expr2  tex2html_wrap_inline21421  [ , tex2html_wrap_inline21419  expr3  tex2html_wrap_inline21421  ]
  
		  tex2html_wrap_inline21419  exec-stmts  tex2html_wrap_inline21421 

END DO

The loop can be named and the tex2html_wrap_inline21419  exec-stmts tex2html_wrap_inline21421 could contain EXIT or CYCLE statements, however, a WHILE clause cannot be used but this can be simulated with an EXIT statement if desired.

The number of iterations, which is evaluated before execution of the loop begins, is calculated as

 
MAX(INT(( tex2html_wrap_inline21419  expr2  tex2html_wrap_inline21421 - tex2html_wrap_inline21419  expr1  tex2html_wrap_inline21421 + tex2html_wrap_inline21419  expr3  tex2html_wrap_inline21421 )/ tex2html_wrap_inline21419  expr3  tex2html_wrap_inline21421 ),0)

in other words the loop runs from tex2html_wrap_inline21419  expr1 tex2html_wrap_inline21421 to tex2html_wrap_inline21419  expr2 tex2html_wrap_inline21421 in steps of tex2html_wrap_inline21419  expr3 tex2html_wrap_inline21421 . If this gives a zero or negative count then the loop is not executedgif.

If tex2html_wrap_inline21419  expr3 tex2html_wrap_inline21421 is absent it is assumed to be 1.

The iteration count is worked out as follows (adapted from the standard, [1]):

  1. tex2html_wrap_inline21419  expr1 tex2html_wrap_inline21421 is calculated,
  2. tex2html_wrap_inline21419  expr2 tex2html_wrap_inline21421 is calculated,
  3. tex2html_wrap_inline21419  expr3 tex2html_wrap_inline21421 , if present, is calculated,
  4. the DO variable is assigned the value of tex2html_wrap_inline21419  expr1 tex2html_wrap_inline21421 ,
  5. the iteration count is established (using the formula given above).

The execution cycle is performed as follows (adapted from the standard):

  1. the iteration count is tested and if it is zero then the loop terminates.
  2. if it is non zero the loop is executed.
  3. (conceptually) at the END DO the iteration count is decreased by one and the DO variable is incremented by tex2html_wrap_inline21419  expr3 tex2html_wrap_inline21421 gif.
  4. control passes to the top of the loop again and the cycle begins again.

For example,

    DO i = 1, 100, 1
     ...
    END DO

is a DO loop that will execute 10 times, it is exactly equivalent to

    DO i = 1, 100
     ...
    END DO

More complex examples may involve expressions and loops running from high to low:

    DO i1 = 24, k*j, -1
     DO i2 = k, k*j, j/k
      ...
     END DO
    END DO

An indexed loop could be achieved using an induction variable and EXIT statement, however, the indexed DO loop is better suited as there is less scope for error.

The DO variable cannot be assigned to within the loop.




next up previous
Next: Examples of Loop Counts Up: Control Flow Previous: Named and Nested Loops

Adam Marshall ©University of Liverpool, 1996
Wed Oct 9 17:57:29 BST 1996