FORTRAN GOTO 作用域

5

我有一个传统的Fortran代码,其中有许多像“goto 50”这样的语句。我想知道goto的目标是全局还是局部的。我的意思是,如果多个函数都有一个目标“50”,那么goto指向哪里。

谢谢您的回答。

2个回答

9

语句标签(例如“50”)必须在当前的“作用域单元”中定义,这基本上可以在这个上下文中翻译为goto调用所在的子程序/函数(如果调用在主程序中,则为主程序)。

因此,例如,在以下程序中,主程序和两个包含的子程序都有自己的标签50,并且goto跳转到它们各自的行50。

program testgotos
    implicit none

    goto 50
    call second
 50 call first
    call second

contains

    subroutine first
    integer :: a = 10

    goto 50
    a = 20
 50 print *,'First: a = ', a

    end subroutine first

    subroutine second
    integer :: a = 20

    goto 50
    a = 40
 50 print *,'Second: a = ', a

    end subroutine second

end program testgotos

6

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