HP 3000 Manuals

DO Statement (Executable) [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Reference

DO Statement (Executable) 

A DO statement defines the beginning of a DO loop.  DO loops are groups
of statements that are executed repeatedly zero or more times, or a list
within one statement that is executed a specified number of times.

The maximum level to which DO statements, or a mixture of IF and DO
statements, can be nested is 20.  Exceeding this number makes programs
unnecessarily complicated and can cause internal compiler errors stating
that the statement is too complicated.

[]
----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | label | Statement label of an executable | Referenced statement terminates | | | statement. | the DO loop. | | | | | ----------------------------------------------------------------------------------------------- | | | | | index | Loop control variable. | Must be a simple variable of an | | | | integer or real data type. | | | | | ----------------------------------------------------------------------------------------------- | | | | | init | Expression that is the initial | Must be an integer or real | | | value for index. | expression. | | | | | ----------------------------------------------------------------------------------------------- | | | | | limit | Expression that is the termination | Must be an integer or real | | | value for index. | expression. | | | | | ----------------------------------------------------------------------------------------------- | | | | | step | Expression that is the increment | Must be an integer or real | | | for index after each execution of | expression. step should not equal | | | the DO loop. step can be positive | 0. If it does, an error occurs if | | | or negative. Its default value is | the RANGE directive is ON. If | | | one. | RANGE is OFF, an infinite loop | | | | could result. | | | | | ----------------------------------------------------------------------------------------------- Semantics DO loops are grouped into four categories: * Labeled DO loops * Block DO loops * Implied DO loops * DO-WHILE loops A labeled or block DO loop executes a group of statements a specified number of times. An implied DO loop is similar to a labeled DO loop, but it is used in a READ, WRITE, PRINT, or DATA statement. A DO-WHILE loop executes a group of statements while a specified condition is true. Labeled and Block DO Loops The labeled and block DO statements control execution of groups of statements by causing the statements to be repeated a specified number of times. The DO statement defines this repetition, or loop. The repeated statement or group of statements is known as the range of the DO loop.
[]
Semantics In a labeled DO loop, the statement with the label must follow the DO statement in the sequence of statements within the same program unit. In a block DO loop, the label is omitted and a following END DO statement terminates the loop. The block DO loop is an extension to the ANSI 77 standard. init, limit, and step are indexing parameters as well as arithmetic expressions. index, init, limit, and step should all be of the same type. If they are not, init, limit, and step are converted to the same type as index. This can sometimes produce unexpected results, as shown in the examples. Labeled DO Loop. A labeled DO loop begins with a DO statement that specifies the label of the terminating statement of the loop. The terminating statement of a labeled DO loop must follow the DO statement. It must not be one of the following: * Another DO statement * A DO-WHILE statement * An assigned GOTO statement * An unconditional GOTO statement * An arithmetic IF statement * Any of the four statements associated with the block IF statement: * IF-THEN statement * ELSE statement * ELSE IF statement * ENDIF statement * A RETURN statement * A STOP statement * An END statement * Any nonexecutable statement The terminating statement of a labeled DO loop can be a logical IF statement. A labeled DO loop can be terminated with an END DO statement. As in all labeled DO loops, this terminating END DO statement must have a label that matches the label of the DO statement. Examples Notes -------------------------------------------------------------------------------------- DO 100 i = 1,10 The group of statements terminating with the one : labeled 100 is repeated 10 times. 100 CONTINUE DO 200 j = 1,10,2 The group of statements terminating with the one : labeled 200 is repeated five times. 200 IF (a(j) .EQ. 0) STOP DO 300 r = 1.0,2.0,.1 The group of statements terminating with the one : labeled 300 is repeated 11 times. Although this 300 END DO loop ends with an END DO statement, it is not a block DO loop. Notice that the label in the DO statement corresponds with the one on the END DO statement. DO 10 i = 1,10,2 Error. Attempted modification of i in this example WRITE (6,'("i =",I2)')i produces a compilation error. i = i-2 10 CONTINUE Block DO Loop. A block DO loop, an extension to the ANSI 77 standard, functions the same as a labeled DO loop. It differs in not using a label in its DO statement. Each block DO loop must be terminated with an END DO statement, which does not require a label. Block DO loops can be nested (as described in "Nesting DO Loops" later in this section), but each level of nesting must be terminated by a separate END DO statement. Examples Notes -------------------------------------------------------------------------------------- DO j = 10,1,-2 Block DO loop. The group of statements terminating : with the END DO statement is repeated five times. END DO DO j = 10,1,2 Block DO loop. The group of statements terminating : with the END DO statement is not executed. (The DO END DO loop is skipped entirely unless the ONETRIP directive is ON.) DO Loop Execution When a DO statement is executed, the following actions occur: 1. limit and step are evaluated, then index and init are evaluated. If necessary, init, limit, and step are converted to the same type as index. The value of init is assigned to index. 2. If the number of times the loop would execute is negative or zero, the loop is skipped and control transfers to the statement following the termination statement of the DO loop. This occurs when init exceeds limit and step is positive, or init is less than limit and step is negative. 3. The range of the loop is executed. 4. index is incremented by the value of step. Note that this is done before testing if the loop has been executed the correct number of times. 5. If the loop has been executed fewer than the correct number of times, steps 3 through 5 are repeated. Within the range of a DO loop, modification of init, limit, or step does not affect the number of iterations of the loop, because these values are established when the loop is entered. Modification of index within the range of the loop is not permitted. Refer to the examples above under "Labeled DO Loop" for such an attempt. Upon normal completion of the DO loop, the value of the control variable is defined to be the next value assigned as a result of the incrementation. For example, in the loop: DO i=1,5 : END DO the value of i after normal completion of the loop is 6. In the loop: DO i=1,10,3 : END DO the value of i after normal completion of the loop is 13. Upon premature exit from the DO loop, the control variable retains its value at the time of exit. Implied DO Loop Implied DO loops are found in input/output statements (READ, WRITE, and PRINT) and in DATA statements. An implied DO loop contains a list of data elements to be read, written, or initialized, and a set of indexing parameters. Inner loops can use the indexes of outer loops. For DATA statements, only integer index variables and expressions can be used. For READ, WRITE, and PRINT statements, real index variables are also permitted. Examples DATA a, b, (vector(i), i = 1,10), k /2.5,-1.0,10*0.0,999/ DATA ((matrix(i,j), i = 0,5), j = 5,10) /36*-1/ The syntax of implied DO loops in DATA statements, is described in "DATA Statement (Nonexecutable)" . The syntax of implied DO loops in input/output statements is described below. Implied DO Loops in Input/Output Statements
[]
----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | index | Loop variable that controls the | Must be a simple variable whose | | | number of times the preceding | type is integer or real. | | | element list is read or written. | | | | | | ----------------------------------------------------------------------------------------------- | | | | | init | Expression that is the initial | Must be an integer or real | | | value for index. | expression. | | | | | ----------------------------------------------------------------------------------------------- | | | | | limit | Expression that is the termination | Must be an integer or real | | | value for index. | expression. | | | | | ----------------------------------------------------------------------------------------------- | | | | | step | Expression that is the increment | Must be an integer or real | | | for index after each execution of | expression. step should not equal | | | the DO loop. step can be positive | 0. If it does, an error occurs if | | | or negative. Its default value is | the RANGE directive is ON. If | | | one. | RANGE is OFF, an infinite loop | | | | could result. | | | | | ----------------------------------------------------------------------------------------------- Semantics The implied DO loop acts like a labeled or block DO loop. The range of the implied DO loop is the list of elements to be input or output. The implied DO loop can transfer a list of simple variables, array elements, or any combination of allowable data elements. The control variable (index) is assigned the value of init at the start of the loop. Execution continues as for DO loops. Implied DO loops can also be nested. Nested implied DO loops follow the same rules as other nested DO loops. For example, the statement: WRITE (6,*) ((a(i,j), i = 1,2), j = 1,3) produces the following output: a(1,1) a(2,1) a(1,2,) a(2,2) a(1,3) a(2,3) The first, or nested, DO loop is satisfied once for each execution of the outer loop. (Refer to "Nesting DO Loops" later in this section.) Collapsed Implied DO Loop. If an implied DO loop meets certain criteria, the DO loop is collapsed and only one internal I/O call is required. The reduced number of I/O calls can yield significantly faster execution time. An implied DO loop is collapsed if the initial index value is less than or equal to the final index value and if the step value is less than or equal to one. DO-WHILE Statement (Executable) As a MIL-STD-1753 standard extension to the ANSI 77 standard, the DO-WHILE statement controls execution of a group of statements by causing the statements to be repeated while a logical expression is true. The DO-WHILE construct is an important element of structured programming.
[]
Each DO-WHILE loop must be terminated by a separate END DO statement, which does not require a label. Note that if the DO-WHILE statement uses the label option, the END DO statement that terminates the DO loop must have a label, and the two labels must match. A DO-WHILE loop evaluates as follows: the logical expression is evaluated and tested at the beginning of the DO loop. If the expression evaluates to true, the group of statements between the DO-WHILE statement and the corresponding END DO statement, referred to as the range of the DO-WHILE loop, is executed and the logical expression is tested again. If the logical expression evaluates to false, the DO-WHILE loop terminates and execution continues with the statement following the END DO statement. The rules for transfers into the range of a DO-WHILE loop are the same as for other DO loops. (Refer to "Ranges of DO Loops" later in this section.) Examples Notes --------------------------------------------------------------------------------------- DO WHILE (i .NOT. 999) Repeatedly reads input until entry of a READ(5,33) i terminating flag (999 in this example). : END DO index = 1 DO WHILE (array(index) .NE. value + .AND. index .LE. limit) index = index + 1 END DO Nesting DO Loops DO loops can contain other DO loops. This is called nesting. The only restriction is that each level (that is, each successive loop) must be completely contained within the preceding loop. In a labeled DO loop, the last statement of an inner (nested) loop must either be the same as, or occur before, the last statement of the outer loop. (For programming clarity, always use a separate terminating statement for each loop.) DO loops can be nested as long as the range of statements in any DO loop does not overlap the range of the preceding loop. Refer to the examples for an illustration of such an illegal construction. Combinations of DO loops and IF blocks can be nested to a depth that is system dependent. Ranges of DO Loops The range of the DO loop is defined as the first statement following the DO statement up to and including the terminating statement defined by label or, in the absence of label, up to and including the next unmatched END DO statement. A DO loop can be exited at any time. Normally, a DO loop is exited when the loop has been completed. Control continues at the statement following the loop's termination statement. A DO loop can be exited prematurely with, for example, a GOTO statement that transfers control out of the loop. It is illegal to transfer control into the range of a DO loop from outside the range unless you have previously jumped out of the loop. The following example shows an illegal construction, one in which the ranges of two loops overlap.
[]
Here is an example that shows the unexpected results possible when index, init, limit, and step are not all of the same type. If they are not, init, limit, and step are converted to the same type as index. Example Notes -------------------------------------------------------------------------------------- DO i = 1,3,.1 The programmer intends to increment i by 10ths. WRITE (6,*) i Instead, when .1 is converted to type integer, it END DO becomes zero, which creates an error because step must not be zero. Extended Range DO Loop As an extension to the ANSI 77 standard, the range of a DO loop can be extended outside the loop. A control statement in the DO loop can transfer control out of the loop, and, after execution of any number of statements, control can branch back into the DO loop. The return to the DO loop must be made from a statement in the extended range of the loop. A DO loop index cannot be modified in the loop's extended range. The following example illustrates the use of an extended range DO loop:
[]
END DO Statement (Executable) The END DO statement terminates a block DO or DO-WHILE loop.
[]
If used to terminate a labeled DO loop, the END DO statement must be labeled. See the examples under "Labeled DO Loop".


MPE/iX 5.0 Documentation