SuivantPrec.Bas prec.BasNiv. sup.

8.2 IF-Blocks 

The simplest form of IF-block looks like this:
       IF(N .NE. 0) THEN
          AVERAG = SUM / N
          AVGSQ  = SUMSQ / N
       END IF
The statements in the block are only executed if the condition is true. In this example the statements in the block are not executed if N is zero in order to avoid division by zero.

The IF-block can also contain an ELSE statement to handle the alternative:
       IF(B**2 .GE. 4.0 * A * C) THEN
           WRITE(UNIT=*,FMT=*)'Real roots'
       ELSE
           WRITE(UNIT=*,FMT=*)'No real roots'
       END IF
Since the IF statement contains a logical expression its value can only be true or false, thus one or other of these blocks will always be executed.

If there are several alternative conditions to be tested, they can be specified with ELSE IF statements:
       IF(OPTION .EQ. 'PRINT') THEN
            CALL OUTPUT(ARRAY)
       ELSE IF(OPTION .EQ. 'READ') THEN
                                                                  
                                                                  
            CALL INPUT(ARRAY)
       ELSE IF(OPTION .EQ. 'QUIT') THEN
            CLOSE(UNIT=OUT)
            STOP 'end of program'
       ELSE
            WRITE(UNIT=*,FMT=*)'Incorrect reply, try again...'
       END IF
There can be any number of ELSE IF blocks but in each case one, and only one, will be executed each time. Without an ELSE block on the end an nothing would have happened when an invalid option was selected.

Block-IF General Rules 

The general form of the block-if structure is as follows:
       IF( logical-expression ) THEN
             a block of statements
       ELSE IF( logical-expression ) THEN
             another block of statements
       ELSE
             a final block of statements
       END IF
The IF THEN, ELSE IF, and ELSE statements each govern one block of statements. There can be any number of ELSE IF statements. The ELSE statement (together with its block) is also optional, and there can be at most one of these.

The first block of statements is executed only if the first expression is true. Each block after an ELSE IF is executed only if none of the preceding blocks have been executed and the attached ELSE IF expression is true. If there is an ELSE block it is executed only if none of the preceding blocks has been executed.

After a block has been executed control is transferred to the statement following the END IF statement at the end of the structure (unless the block ends with some statement which transfers control elsewhere).

Any block can contain a complete block-IF structure properly nested within it, or a complete DO-loop, or any other executable statements (except END).

It is illegal to transfer control into any block from outside it, but there is no restriction on transferring control out of a block.

The rules for logical expressions are covered in section 7.7.

Guidelines 

The indentation scheme shown in the examples above is not mandatory but the practice of indenting each block by a few characters relative to the rest of the program is strongly recommended. It makes the structure of the block immediately apparent and reduces the risk of failing to match each IF with an END IF. An indenting scheme is especially useful when IF-blocks are nested within others. For example:
       IF(POWER .GT. LIMIT) THEN
           IF(.NOT. WARNED) THEN
               CALL SET('WARNING')
               WARNED = .TRUE.
           ELSE
               CALL SET('ALARM')
           END IF
       END IF
The limited width of the statement field can be a problem when IF-blocks are nested to a very great depth: but this tends to mean that the program unit is getting too complicated and that it will usually be beneficial to divide it into subroutines. If you accidentally omit an END IF statement the compiler will flag the error but will not know where you forgot to put it. In such cases the compiler may get confused and generate a large number of other error messages.

When an IF-block which is executed frequently contains a large number of ELSE IF statements it will be slightly more efficient to put the most-likely conditions near the top of the list as when they occur the tests lower down in the list will not need to be executed.

SuivantPrec.Bas prec.HautNiv. sup.