英文:
constant number before a statement in fortran code
问题
我遇到了一个问题,找不到任何解释,考虑以下两个代码片段。
这个可以运行
Program Hello
implicit none
print *, "hello world"
1 print *, "one"
print *, "bye world"
End Program Hello
输出:
hello world
one
bye world
这个无法编译
Program Hello
implicit none
print *, "hello world"
1
print *, "one"
print *, "bye world"
End Program Hello
错误:\
main.f95:16:1:\
16 | 1\
| 1\
错误:在(1)处没有语句的语句标签
有人知道这里发生了什么吗?为什么第一个可以编译和运行?
PS:这不是有用的代码,但是我在一些Fortran代码中看到了“1 continue”,只是想知道那到底实现了什么?
编辑:所以很可能是一个语句标签,Fortran语句标签是如何使用的?它们只是装饰性的还是实际提供了一些用途?
英文:
I came across something and I am unable to find any explanation for it, consider the following two code snippets.
THIS RUNS
Program Hello
implicit none
print *, "hello world"
1 print *, "one"
print *, "bye world"
End Program Hello
output:
hello world
one
bye world
THIS DOES NOT COMPILE
Program Hello
implicit none
print *, "hello world"
1
print *, "one"
print *, "bye world"
End Program Hello
error:\
main.f95:16:1:\
16 | 1\
| 1\
Error: Statement label without statement at (1)
Anyone knows what is happening here? Why does the first one compile and run?
PS: it is not useful code, however I saw "1 continue" in some Fortran code, and was just wondering what that even achieves?
EDIT: SO it is likely a statement label, How are Fortran statement labels used? are they purely cosmetic or actually provide some usage?
答案1
得分: 4
1
在这里不是一个数值常数;它是一个_语句标签_。(回顾一下,表达式,比如实际的常数1
,不能随意出现在任意位置,或者作为语句的唯一部分,这在其他一些语言中是可以找到的。)
语句标签(Fortran 2018 6.2.5):
>提供了一种引用单个语句的方法
就是这样:它是一个标签,表示第一个示例中的print
语句可以被引用为标签1
的引用。
语句标签由一个到六位数字组成,其中至少有一位不是零。在一个作用域内,它们必须是唯一的(忽略前导零)。
标签后面必须跟着一个非空白字符的语句。这使得
1
无效。
并不是所有的语句都可以加标签,并且并不是所有的语句都有用的标签。标签只在以下情况下对程序有意义:
- 作为一个可以“跳转”到的语句(例如
go to
等)。 - 必须作为
format
语句的一部分(这样它就可以被引用)。 - 在非块DO结构中(在Fortran 2018中已经过时)。
在其他情况下,语句标签只对程序员有意义(比如作为文档)。
1 continue
在非块DO的情况下通常用于如下情况:
do 1 i=1, 10
1 continue
(但是我们不再使用这种方式了参考链接)。
或者类似地作为一个无用的跳转目标:
read(unit, err=1)
...
1 continue
(如果我们没有使用iostat=...
的话,我们应该使用)。
英文:
The 1
here is not a numeric constant; it is a statement label. (Recall that expressions like the actual constant 1
cannot appear in arbitrary places, or as the only part of a statement as can be found in some other languages.)
A statement label (Fortran 2018 6.2.5):
> provides a means of referring to an individual statement
That's it: it's a label to say that the print
statement in the first example can be referred to be a reference to the label 1
.
A statement label consists of between one and six digits, at least one of which is not a zero. Within a scope, they must be unique (ignoring leading zeros).
A label must be followed by a statement with non-blanks. This makes
1
invalid.
Not all statements may be labelled, and not all statements are usefully labelled. Labels are meaningful to the program only in the following cases:
- As a statement to be "jumped" to (
go to
, etc.) - Necessarily as part of a
format
statement (so it can be referenced) - In a non-block DO construct (obsolete in Fortran 2018)
In other cases a statement label is meaningful only to programmers (say as documentation).
1 continue
is commonly used in cases like the non-block DO:
do 1 i=1, 10
1 continue
(but we don't do these any longer).
Or similarly as a do-nothing jump target:
read(unit, err=1)
...
1 continue
(if we aren't using iostat=...
which we should).
答案2
得分: 2
Fortran 2018中关于语句标签的部分:
6.2.5 语句标签
2 如果语句有标签,该语句必须包含一个非空字符。...
你不能在空行上使用标签,这在Fortran中是无效的。你可以在标签后加上CONTINUE
语句,它不执行任何操作。
在旧的源代码中,经常会发现带有标签的CONTINUE
语句用于终止DO
循环。
除此之外,语句标签也常常用于FORMAT
语句和作为GO TO
语句的目标。还有其他不太常见的用法。
所以,关于你的编辑:
DO 10 I = 1,99
PRINT *, I
10 CONTINUE
是一个DO循环,而
GO TO 10
是跳转到该语句的结束行。
英文:
Fortran 2018 in the section about statement labels:
> 6.2.5 Statement labels
>
> 2 If a statement is labeled, the statement shall contain a nonblank
> character. ...
You cannot have a label on an empty line, it is not valid Fortran. You can put the CONTINUE
statement to your lable, it does nothing.
It is quite common to find CONTINUE
statements with labels terminating a DO
loop in old source codes.
Other than that, stament labels were also commonly used for FORMAT
statements and as targets of GO TO
. There are other, less common, use cases too.
So, regarding your edit:
DO 10 I = 1,99
PRINT *, I
10 CONTINUE
is a do loop and
GO TO 10
is a jump to that end line with the statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论