Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EXIT LABELED DO LOOP FORTRAN 90

Status
Not open for further replies.

NikolaStarcevic

Programmer
Jun 6, 2017
8
RS
OK guys, this is funny....
I have nested DO LOOP:

PROGRAM RBX
IMPLICIT REAL*8 (A-H,O-Z) !variables that start with letter a,b,c,d,e,f,g,h,o,p,q,r,t,s,... are doubles
DO 1 I=1,5,1
DO 2 J=1,10,1
Z(I,J)=(some very complicated mathematics)
WRITE(*,*) Z(I,J)
2 CONTINUE
1 CONTINUE
END PROGRAM RBX

Ok, this way i will have array of 50 Z's.
But this variable "Z" can be determinated for only few values of "I" and "J", so on the end i will have 3 values of Z displayed on screen (thats just mathematics, it always find three "Z" but each have different value).
But "Z" isnt zero for other values of "I" and "J" in loop, it just doesnt exist, empty space, no value, NaN...
I.e. this variable "Z" is evalueted for every increment in LOOPs but it doesnt always have value.

I want to stop both DO LOOPs when Z got 1st value and after is displayed on screen.
I tryed: IF (Z.GT.0) EXIT after WRITE command but it doesnt work correctly, this way program display 1st value on screen and it doesnt display second and third value which is good, but both LOOP continues.
But i want to stop/exit both LOOPs after 1st value is displayed, because this "some very complicated mathematics" is 100 lines of code and last for 10 minutes each time i start program.

Any idea?

Thanks,
Nikola
 
btw, I tryed also IF(condition) CYCLE, IF(condition) ENDDO, IF(condition) GOTO 1, IF(condition) GOTO 2, IF(condition) GOTO END, IF(condition) END PROGRAM RBX, IF(condition).
 
You could use a goto
Code:
    do i = 1, 5, 1
        do j = 1, 10, 1
            ! whatever
            if (conditionmet) goto 10
        end do
    end do
10  continue
 
NikolaStarcevic said:
I just tryed that but nothing happends... loop continues...
If you would have only 2 loops as you described above, then the jump would work.
But maybe your program is more complicated, maybe you have other loop which is realized with other goto...etc

Other option is to use the STOP statement, e.g:

Code:
[COLOR=#800080]program[/color] stop_example 
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, j

  [COLOR=#a52a2a][b]do[/b][/color] i [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]5[/color], [COLOR=#ff00ff]1[/color]
        [COLOR=#a52a2a][b]do[/b][/color] j [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]10[/color], [COLOR=#ff00ff]1[/color]
            [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color],[COLOR=#ff00ff]'("i = ", I2, ", j = ", I2)'[/color]) i, j
            [COLOR=#a52a2a][b]if[/b][/color] (j [COLOR=#a52a2a][b].eq.[/b][/color] [COLOR=#ff00ff]1[/color]) [COLOR=#a52a2a][b]stop[/b][/color] [COLOR=#ff00ff]'test'[/color]
        [COLOR=#a52a2a][b]end do[/b][/color]
  [COLOR=#a52a2a][b]end do[/b][/color]
[COLOR=#800080]end program[/color]

Output:
Code:
$ gfortran stop_example.f95 -o stop_example

$ ./stop_example 
i =  1, j =  1
STOP test


 
I just tryed that but nothing happends... loop continues...

What was your termination condition? Maybe it was never satisfied.
 
XWB,
my condition was IF (Z.GT.0), so it should stop loop when 1st value of Z greater than zero is found, but it just stop writing Z after that first value and continue loop. I also tryed bunch of other conditions and exactly same happends when condition is satisfied.
Just dont know what to do...
 
MIKROM
this command STOP did good job. Yet there is one problem, it literaly stop program :) i cant put entire code in main loop:

program stop_example
integer :: i, j
DO 1 X=1,10,1
do i = 1, 5, 1
do j = 1, 10, 1
write(*,'("i = ", I2, ", j = ", I2)') i, j
if (j .eq. 1) stop 'test'
end do
end do
1 CONTINUE
end program

this way main loop with increment X wont be executed
 
You can't use

if z .gt. 0

you have to do

if Z(I,J) .gt. 0




Bill
Lead Application Developer
New York State, USA
 
4Jul said:
But i want to stop/exit both LOOPs after 1st value is displayed,
7Jul said:
it should stop loop when 1st value of Z greater than zero is found, but it just stop writing Z after that first value and continue loop.
You have two conflicting requirements here. Do you want to exit both loops or do you want to just print the first occurrence and not exit?
 
Code:
program rbx
integer i, j
real*8 z(5,10)
z=0.0
outer: do i = 1, 5, 1
         do j = 1, 10, 1
           ! complicated mathematics to calculate Z(i,j)

           ! test Z(i,j)...but do it correctly; for example, can Z(i,j) be negative?
           if ( z(i,j) > 1.0e-06 ) then
             write(*,'("i=",i2,"  j=",i2,"  z(i,j)=",e10.4)') i,j,z(i,j)
             exit outer
           end if
         end do
       end do outer
end program rbx
 
XWB,
I want to print first occurence and to exit loop just after.
I will try Salgerman's solution now, it look it work ok.
Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top