我有一个很长的条件语句来确定是否退出我在几个地方使用的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
是否有办法通过Fortran中的子例程或函数返回exit
语句,如:
call check_for_exit
很高兴不必须添加变量等等(例如,从函数返回一个逻辑,然后我做一个更简单的if
):< / p>
exit_now = check_exit()
if (exit_now == .true.) exit
答案 0 :(得分:1)
为什么不简单地使用像您的示例中的逻辑函数并直接测试它(没有其他变量):
if ( check_exit() ) exit
或者,如果你想使用整数,例如负整数表示错误,正数表示警告:
if ( complicated_check() > 0 ) then
print *, 'WARNING'
elseif( complicated_check() < 0 ) then
exit
endif
虽然没有辅助变量就会开始效率低下; - )
答案 1 :(得分:0)
如果你想处理多个返回值,假设f90 +你可以使用select case
来避免辅助变量(或者调用函数两次)
select case ( complicated_check() )
case (1:)
print*,'Warning'
case (:-1)
exit
end select