Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 10 HP Fortran statements

DO

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

Controls execution of DO loop.

Syntax

[ construct-name :] DO [ label ] [ loop-control ]
construct-name

is the name given to the DO construct. If construct-name is specified, an END DO statement must appear at the end of the DO construct and have the same construct-name.

label

is the label of an executable statement that terminates the DO loop. If you specify label, you can terminate the DO loop either with an END DO statement or with an executable statement; the terminating statement must include label. If you do not specify label, you must terminate the DO loop with the END DO statement.

loop-control

is information used by the DO statement to control the loop. It can take one of the following forms:

  • index = init, limit [, step]

  • WHILE (logical-expression)

  • loop-control is omitted

In the first form, index is a scalar variable of type integer or real; init, limit, and step are scalar expressions of type integer or real. In the second form, logical-expression is a scalar logical expression. In the third form, loop-control is omitted. If you use the second or third form, you must terminate the DO loop with the END DO statement.

Description

The syntax of the DO statement allows for the following types of DO loops:

  • Counter-controlled loop: a loop count is calculated that controls the number of times the block is executed, unless a prior exit occurs. A loop variable is incremented or decremented after each execution.

  • While loop: a condition (logical-expression) is tested before each execution of the block; when it is false, execution ceases. An exit may occur at any time.

  • Infinite loop: there is no loop-control; repeated execution of the block ceases only when an exit from the loop occurs.

When label is present in the DO statement, it specifies the label of the terminating statement of the DO loop. The terminating statement cannot be any of the following statements:

  • GO TO (unconditional)

  • GO TO (assigned)

  • IF (arithmetic)

  • IF (block)

  • ELSE or ELSE IF

  • END, END IF, END SELECT, or END WHERE

  • RETURN

  • STOP

  • DO

  • Any nonexecutable statement

Note, however, that the terminating statement can be an IF (logical) or an END DO statement.

To maintain compatibility with some older versions of Fortran, you can use the +onetrip compile-line option to ensure that every counter-controlled DO loop in the program executes at least once.

Extended-range DO loops

Extended-range DO loops—a compatibility extension—allow a program to transfer control outside the DO loop’s range and then back into the DO loop. Extended-range DO loops work as follows: if a control statement inside a DO loop transfers control to a statement outside the DO loop, then any subsequent statement can transfer control back into the body of the DO loop.

For example, in the following code, the range of the DO loop is extended to include the statement GOTO 20, which transfers control back to the body of the DO loop:

   DO 50 i = 1, 10
20 n = n + 1
IF (n > 10) GOTO 60
50 CONTINUE ! normally, the range ends here
60 n = n + 100 ! this is the extended range,
GOTO 20 ! which extends down to this line

Examples

The following DO construct displays the integers 1 through 10:

DO i = 1, 10
WRITE (*, *) i
END DO

The next example is a FORTRAN 77-style DO loop that does the same as the preceding example:

   DO 50 i = 1, 10
WRITE (*, *) i
50 CONTINUE

The following DO construct iterates 5 times, decrementing the loop index from 10 to 2:

DO i = 10, 1, -2
END DO

The following is an example of a DO WHILE loop:

DO WHILE (sum < 100.0)
sum = sum + get_num(unit)
END DO

The following example illustrates the use of the EXIT statement to exit from a nested DO loop. The loops are named to control which loop is exited. Note that loop-control is missing from both the inner and outer loops, which therefore can be exited only by means of one of the EXIT statements:

outer:DO
READ *, val
new_val = 0
inner:DO
new_val = new_val + proc_val(val)
IF (new_val >= max_val) EXIT inner
IF (new_val == 0) EXIT outer
END DO inner
END DO outer

The next DO construct never executes:

DO i = 10, 1
END DO

Related statements

CONTINUE, CYCLE, END (construct), and EXIT

Related concepts

For related information, see the following:

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.