INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

EXIT LABELED DO LOOP FORTRAN 90

EXIT LABELED DO LOOP FORTRAN 90

(OP)
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

RE: EXIT LABELED DO LOOP FORTRAN 90

(OP)
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).

RE: EXIT LABELED DO LOOP FORTRAN 90

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 

RE: EXIT LABELED DO LOOP FORTRAN 90

(OP)
I just tryed that but nothing happends... loop continues...

RE: EXIT LABELED DO LOOP FORTRAN 90

Quote (NikolaStarcevic)


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

program stop_example 
  integer :: i, j

  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
end program 

Output:

CODE

$ gfortran stop_example.f95 -o stop_example

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


RE: EXIT LABELED DO LOOP FORTRAN 90

Quote:

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

What was your termination condition? Maybe it was never satisfied.

RE: EXIT LABELED DO LOOP FORTRAN 90

(OP)
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...

RE: EXIT LABELED DO LOOP FORTRAN 90

(OP)
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

RE: EXIT LABELED DO LOOP FORTRAN 90

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

RE: EXIT LABELED DO LOOP FORTRAN 90

Quote (4Jul)

But i want to stop/exit both LOOPs after 1st value is displayed,

Quote (7Jul)

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?

RE: EXIT LABELED DO LOOP FORTRAN 90

CODE --> fortran90

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 

RE: EXIT LABELED DO LOOP FORTRAN 90

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close