Loops 使用子例程退出Fortran中的'do while'循环
我有一个很长的条件语句来决定是否退出我在几个地方使用的Loops 使用子例程退出Fortran中的'do while'循环,loops,fortran,exit,Loops,Fortran,Exit,我有一个很长的条件语句来决定是否退出我在几个地方使用的do while循环 if (long_class_name <= something + 0.5 .and. & long_class_name >= something - 0.5 .and. & long_class_name <= some_other_thing + 0.5 .and. & long_class_name >= some_other_thing
do while
循环
if (long_class_name <= something + 0.5 .and. &
long_class_name >= something - 0.5 .and. &
long_class_name <= some_other_thing + 0.5 .and. &
long_class_name >= some_other_thing - 0.5 .and. &
yet_another_thing == -999.0) exit
最好是不必须添加变量等(例如,从函数返回一个逻辑,如果打开,则执行更简单的):
为什么不直接使用示例中的逻辑函数并直接测试它(无需附加变量):
或者,如果要使用整数,例如负整数表示错误,正整数表示警告:
if ( complicated_check() > 0 ) then
print *, 'WARNING'
elseif( complicated_check() < 0 ) then
exit
endif
如果(复杂的检查()>0)那么
打印*,“警告”
elseif(复杂的检查()<0)然后
出口
恩迪夫
尽管在没有辅助变量的情况下,这开始变得低效;-) 如果要处理多个返回值,假设f90+可以使用选择case
来避免使用辅助变量(或调用函数两次)
使用标志变量是一种相当标准的模式。它使代码在将来更加灵活,还允许您在调试期间处理逻辑。我觉得Shawn C使用标记是正确的。回答得很好。我想我需要更多地研究一下这个概念,以了解在什么情况下什么是最好的。谢谢
exit_now = check_exit()
if (exit_now == .true.) exit
if ( check_exit() ) exit
if ( complicated_check() > 0 ) then
print *, 'WARNING'
elseif( complicated_check() < 0 ) then
exit
endif
select case ( complicated_check() )
case (1:)
print*,'Warning'
case (:-1)
exit
end select