关于fortran90:带有数字/标签的Fortran IF语句,而不是其他语句

Fortran IF statement with numbers/labels rather than another statement

此Fortran代码是什么意思:

1
2
3
4
5
   IF (J1-3) 20, 20, 21
21 J1 = J1 - 3
20 IF (J2-3) 22, 22, 23
23 J2 = J2 - 3
22 CONTINUE

我在旧项目中见过,但我不知道带有数字(标签)的IF意味着什么。


这是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.

对于您问题中的示例,从上面的最后一句话开始,

  • 如果J1-3 < 0语句20将被执行
  • 如果J1-3 = 0语句20也将被执行
  • 如果J1-3 > 0语句21将被执行

编辑:编写此代码的一种现代且更具可读性的方法是:

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