gpt4 book ai didi

fortran90 - 如何在某些情况发生时用消息 BREAK 程序

转载 作者:行者123 更新时间:2023-12-04 05:38:11 24 4
gpt4 key购买 nike

当我的 f90 程序的一个子程序中出现某些情况时,我试图找到一种方法来放置中断订单。
是否有可能从中获得任何想法?代码方案如下所示:

    /
modules
PROGRAM
allocate variables
CALL subroutines for initializing variables
...
do 1,max iterations
CALL subroutine1
CALL subroutine2
CALL subroutine3 !--> here I have the condition
...
...
end do

END PROGRAM

Subroutine subroutine3
...
if (condition = true) then ! what I want to do here is to break the program printing a message saying that it is stopped because condition is true)

end if
end subroutine 3

/

我会很感激你的帮助,

我对fortran很陌生,我是这个论坛的新手!

提前谢谢你,

阿尔伯特·P

最佳答案

if (condition) stop

将立即停止您的程序。你可能更喜欢
if (condition) then
write(*,*) 'A friendly message'
stop
end if

如果您的编译器符合 Fortran 2008,您甚至可以编写
if (condition) stop 'A friendly message'

但是,也许您想要做的不是停止程序而是退出子程序,在这种情况下,您将以某种可接受的方式跳到子程序的末尾。

请注意 condition=true Fortran 将 condition 的值与逻辑常量 .true. 进行比较在语法上不正确.它是一个赋值语句。语法正确的比较是 condition == .true.但这在语义上是有害的,只需写 if (condition)表达了 if (condition == .true.) 的一切确实。缩写形式还表明您是程序员而不是脚本小子。

关于fortran90 - 如何在某些情况发生时用消息 BREAK 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com