next up previous contents
Next: Arrays - DIMENSION, REAL Up: IF statements Previous: Logical IF

   
Block IF

The third form of IF is the BLOCK IF. The general form is:-

        IF (e1) THEN
          block 1
        ELSE IF (e2) THEN
          block 2
        .
        .
        .
        ELSE IF (en) THEN
          block n
        ELSE
          block n+1
        ENDIF

e1,e2..en are logical expressions. block 1, block 2....block n+1 are blocks of FORTRAN statements of arbitrary length. Any executable statement can be contained in a block. Program flow is as follows. If e1 is true then the code in block1 is executed and, (unless the block contains a GOTO to transfer it out of the block) when complete, control passes to the statement after the ENDIF. If e1 is not true then e2 is tested and if true block2 executed. Otherwise e3 is tested and so on. Finally if none of e1,e2...en is true then the code in block n+1 is executed. The number of ELSE IF statements is arbitrary. The ELSE IF and ELSE may be omitted so that the simplest form of block IF is :-

        IF (e1) THEN
          block1
        ENDIF

as a very simple example, the arithmetic IF:-

IF (A) 100,200,300
could be written:-

 
        IF(A.LT.0) THEN
          GOTO 100
        ELSE IF (A.EQ.0.) THEN
          GOTO 200
        ELSE
          GOTO 300
        ENDIF

Exercise 7
Type in and run this simple guessing game program
C       The computer generates a  number between 1 and 1000

C        RAN generates a random number r:  0. <= r <  1.
C        it needs the seed value to be passed to it.  The random
C        number routine on your machine may not be called RAN or
C        have the same call sequence.  Check local documenatation.

        IY=12345
10      NUM=1.+1000*RAN(IY)
 
        PRINT *, 'TRY TO GUESS MY NUMBER'
 
100     READ *,INUM
        IF (INUM.EQ.NUM) THEN
          PRINT *, 'WELL DONE YOU HAVE GUESSED RIGHT!'
110       PRINT *, 'DO YOU WANT ANOTHER GO?'
          PRINT *, 'TYPE 0 FOR NO, 1 FOR YES'
          READ *,INUM
          IF (INUM.EQ.0) THEN
            STOP
          ELSE IF (INUM.EQ.1) THEN
            GOTO 10
          ELSE
            GOTO 110
          ENDIF
        ELSE IF (INUM.LT.NUM) THEN
          PRINT *,'NO - TOO LOW'
        ELSE
          PRINT *,'NO - TOO HIGH'
        ENDIF
        PRINT *,'TRY AGAIN'
        GOTO 100
        END
Several points to take note of. You can have a block IF within another block IF (putting a construct within the same construct is called nesting) so long as it fits completely within the other block. Also you can jump out of a block but cannot jump in. It is a good habit to indent code within a block slightly so as to make it more readable (particularly true if nesting). Finally note a good programming practise - when offering a user a set of possible replies (e.g. 0 or 1 for no or yes) check and reissue the question if an illegal reply is given.
* * *


next up previous contents
Next: Arrays - DIMENSION, REAL Up: IF statements Previous: Logical IF
n west (APC)
2000-03-08