首页 > 解决方案 > Fortran 中的 BLOCK 有什么意义?

问题描述

我正在看一些代码,有这样的:

BLOCK

...lines of code...

END BLOCK

目的是BLOCK什么?我试图用谷歌搜索它,但我发现的只是一些关于BLOCK DATACOMMON块的东西。我不确定它们是否相关。

标签: fortran

解决方案


来自 Fortran 2008 标准:

BLOCK 构造是一个可执行的构造,它可能包含声明。

它与公共块或块数据程序单元无关。

因此,主要用途是这种“包含声明”。

作为范围单位,我们有类似的东西

integer i
block
  integer j ! A local integer
  integer i ! Another i
  save i    ! ... which can even be SAVEd
end block

它提供了声明的位置:

! ... lots of code
block
  integer something
  read *, something
end block
! ... lots more code

这些范围块允许自动对象

integer i
i = 5
block
  real x(i)
end block

作为可执行结构,它们还具有有用的流控制

this_block: block
  if (something) exit this_block
  ! ... lots of code
end block

他们还具有最终控制权:

type(t) x
block
  type(t) y
end block ! y is finalized
end  ! x is not finalized

xy可终结的类型。

哦,让我们不要忘记如何将人们与隐式类型混淆。


推荐阅读