Документ взят из кэша поисковой машины. Адрес оригинального документа : http://star.arm.ac.uk/f77to90/c6.html
Дата изменения: Sat Feb 17 19:42:32 1996
Дата индексирования: Mon Oct 1 20:58:57 2012
Кодировка:

Поисковые слова: sts-64
Control statements

6. Control statements

As conditional or control statements you have IF in many variants (but essentially not changed from Fortran 77), DO (with some new variants) and the completely new statement CASE.

The DO-loop should now be ended with the statement END DO and we no longer need any statement number. In addition, we can use the statement EXIT to jump out of the DO-loop and CYCLE in order to go to the next iteration of the present DO-loop. A DO-loop can be assigned a name, which is done by giving the name before the DO and followed by a colon. In addition the final END DO can be followed by the name of the DO-loop.


            SUMMA = 0.0

     ADAM : DO I = 1, 10

                    X = TAB(I)

     EVA :          DO J = 1, 20

                           IF (X > TAB(J)) CYCLE ADAM

                           X = X + TAB(J)

                    END DO EVA

                    SUMMA = SUMMA + X

                    IF (SUMMA >= 17.0) EXIT ADAM

            END DO ADAM

In the example above, the execution of the inner loop will be interrupted with a jump to the next cycle of the outer loop, and thus the variable sum or SUMMA will not be increased, if X is greater than the given table value. As soon as the sum is at least 17 the outer loop is also interrupted.

If no name is given in the EXIT or CYCLE statements the present inner loop is automatically used. With the present inner loop I mean the one where the EXIT or CYCLE statements being executed are. These statements then replace the GOTO to the final statement, which was often used in the old DO-loop. This final statement usually was an CONTINUE statement.

Also an IF statement can be given a name. In that case the corresponding END IF ought to be followed by that name.

A new construct in standard Fortran is CASE. It however appeared in many Fortran dialects before. It can choose a suitable case for a scalar argument of type INTEGER, LOGICAL or CHARACTER. A simple example is based on an integer IVAR.


 SELECT CASE (IVAR)

    CASE (:-1)                        !  all negative numbers

           WRITE (*,*)  'Negative number'

    CASE (0)                          !  zero case

           WRITE (*,*) ' Zero'

    CASE (1:9)                        !  one-digit number

           WRITE (*,*) ' Digit ', IVAR

    CASE (10:99)                      !  two-digit number

           WRITE (*,*) ' Number ', IVAR

    CASE DEFAULT                      !  all remaining cases

           WRITE (*,*) ' Too great number'

 END SELECT

It is not permitted with overlapping arguments. This means that one single argument may not satisfy more than one of the cases of CASE. The default case does not have to be included. If no valid case is found the execution will continue with the first statement after the END SELECT. I recommend that you include a DEFAULT and then give an error message if an argument has a not permitted value.

It is recommended to use the CASE instead of an assigned or computed GOTO statement, or an arithmetic IF-statement.

Exercises

(6.1) Write a CASE-statement that performs three different calculations depending on whether the variable is negative, zero or any of the first odd prime numbers (3, 5, 7, 11, 13) and performs nothing in all other cases.
Solution.

(6.2) Write a DO-loop that adds the square roots of 100 given numbers, but skips negative numbers and concludes the addition if the present value is zero.
Solution.


Last modified: 16 November 1995
boein@nsc.liu.se