Reputation:
Following minimal example:
L1: do while(.true.)
L2: do while(.true.)
if(something=.true.) exit L1
end do L2
end do L1
So now i want to write the inner Loop L2 into a subroutine within a seperate module to link them together when compiling the code, like gfortran main.f95 innerloop.o -o somename
module innerloop
contains
subroutine innerloop()
L2: do.....
if(something=.true.) exit L1
...
end module innerloop
I get various errors, even when i set the loops name to a global character via a module with global variabels. Because of a huge number of various if cases it is no idea to define a globle variable as a flag and do something like: if(something=.true.) exitvar=.true. and then to write an if-case behind every subroutine or function if the variable is .true. or not and exit all the loops step by step. Is there another way to exit an outer loop from an inner one within a seperate subroutine in de module?
Upvotes: 1
Views: 702
Reputation: 78364
Your approach breaches Fortran's rules concerning the scope of 'jumps', whether they be exit
s or goto
s or similar constructs. In other words you can forget about exit
-ing from a labelled do-loop in the way you propose.
I get various errors, even when i set the loops name to a global character Yes, the construct-name on a do
(or similar) construct is not a character variable or constant or any other thing that can be set during execution.
This, Fortran's sanity on the scoping of jumps, is a good thing which makes it harder to write spaghetti code. You don't want to use a global variable as a flag, nor should you want to. Use a local variable, perhaps something like this
module innerloop
contains
subroutine innerloop(l1return)
logical, intent(out) :: l1return
l1return = .false.
L2: do.....
if (something) then
l1return = .true.
exit L2
end if
...
end module innerloop
Yes, you now have to test whether the subroutine finished early or not, but that is a small price to pay to avoid the madness that follows from unconstrained jumping from scope to scope.
Upvotes: 4