BLOCK construct (Fortran 2008)

The BLOCK construct defines an executable block that can contain declarations.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-BLOCK_statement---------------------------------------------><

Read syntax diagramSkip visual syntax diagram
>>-+--------------------+--------------------------------------><
   '-specification_part-'   

Read syntax diagramSkip visual syntax diagram
>>-statement_block---------------------------------------------><

Read syntax diagramSkip visual syntax diagram
>>-END_BLOCK_statement-----------------------------------------><

BLOCK_statement
See BLOCK (Fortran 2008) for syntax details.
END_BLOCK_statement
See END (Construct) for syntax details.

To terminate execution of a BLOCK construct, you can use an EXIT statement. To branch out of a BLOCK construct, you can use a GOTO (unconditional) statement.

A local variable of a BLOCK construct within a pure subprogram cannot have the SAVE attribute.

COMMON, EQUIVALENCE, IMPLICIT, INTENT, NAMELIST, OPTIONAL, statement function, and VALUE statements are not allowed in the specification part of the BLOCK construct.

A common block name cannot be specified in a saved entity list in the BLOCK construct.

You can transfer control within a BLOCK construct, or from inside to outside of a BLOCK construct. You can transfer control from outside to inside of a BLOCK construct only when the control is returned from a procedure call inside the BLOCK construct.

Examples

Example 1: The following example shows that a BLOCK construct can be specified with an optional name and nested within another BLOCK construct.
PROGRAM foo
  INTEGER :: a
  
  add1 : BLOCK            
    INTEGER :: res1
    res1 = a + 1
    ! The BLOCK statement has no BLOCK_construct_name
    BLOCK
      INTEGER :: res2
      res2 = res1 + 1
    END BLOCK
  ! The END BLOCK statement must have the same BLOCK construct name 'add1'         
  END BLOCK add1    
END PROGRAM foo     
Example 2: You cannot transfer control from outside a BLOCK construct to inside the BLOCK construct, except for the return from a procedure call inside the construct. For example:
PROGRAM main
  INTEGER :: a
  
  a = 5
  BLOCK
    INTEGER :: b
    b = a + 2
    ! Program control is returned from a procedure call inside the BLOCK construct
    CALL Sub(b)
  END BLOCK
END PROGRAM main

SUBROUTINE Sub(B)
  INTEGER :: b
  
  b = b * b
  PRINT *, b
END SUBROUTINE Sub
Example 3: The following example shows how an unconditional GOTO statement is used to exit a BLOCK construct.
   PROGRAM foo 
     BLOCK
       INTEGER :: i
       i = 1
       ! Before the BLOCK construct is exited, local pointers are
       ! nullified, local allocatables are deallocated, and local
       ! finalizable objects are finalized.
       GOTO 10          
       i = i + 1
     END BLOCK
10   PRINT *, i
   END PROGRAM foo

Related information