Fortran中的IF语句,使用数字/标签而不是另一个语句

10

这段Fortran代码是什么意思:

   IF (J1-3) 20, 20, 21
21 J1 = J1 - 3
20 IF (J2-3) 22, 22, 23
23 J2 = J2 - 3
22 CONTINUE

我在旧的项目中看到了这个带有数字(标签)的IF语句,但我不知道它的含义。


3
在FORTRAN早期,这并不奇怪——因为那时它是该语言中唯一可用的条件语句。 - Hristo Iliev
1个回答

13
这是一条来自FORTRAN 77的算术if语句。改编自FORTRAN 77规范(重点在于我):
The form of an arithmetic IF statement is: IF (e) s1, s2, s2 where: e is an integer, real, or double precision expression s1, s2, and s3 are each the statement label of an executable statement that appears in the same program unit as the arithmetic IF statement. The same statement label may appear more than once in the same arithmetic IF statement. Execution of an arithmetic IF statement causes evaluation of the expression e followed by a transfer of control. The statement identified by s1, s2, or s3 is executed next as the value of e is less than zero, equal to zero, or greater than zero, respectively.
For the example in your question, from the last sentence above,
  • 如果 J1-3 < 0,将执行第20条语句
  • 如果 J1-3 = 0,也将执行第20条语句
  • 如果 J1-3 > 0,将执行第21条语句

编辑:一种现代且更易读的编写方式是:

if (J1-3 > 0) J1 = J1 - 3
if (J2-3 > 0) J2 = J2 - 3

没问题。请注意,我的编辑仍然适用于FORTRAN 77(当然也适用于Fortran 90、95等)。它使用了一个逻辑if语句。另一种类型的if语句是块if语句 - Chris
4
小写的 if 不是标准的 FORTRAN 77 :) - Hristo Iliev
1
Fortran曾经是不区分大小写的...确实..至少在打孔卡之后 ;) - mariotti
1
在现代版本中,将J1与3进行比较是否更易读?J1>3 至少,如果您知道J1没有溢出其类型。 - James

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接