2

I am working on a piece of legacy F77 code and trying to convert it to equivalent F90 code. I ran into these lines below and could some one advise if my conversion is correct?

Fortran 77 code:

Subroutine area(x,y,z,d)
do 15 j=1,10
if (a.gt.b) go to 20
15 CONTINUE
20 Statement 1
   Statement 2
   Statement 3
end subroutine

I tried to convert it to F90 and came up as below:

Subroutine area(x,y,z,d)
  dloop: do j=1,10
    if (a>b) then 
      statement 1
      statement 2
      statement 3
    else
      write(*,*) 'Exiting dloop'
      exit dloop
    end if
  end do dloop
end subroutine

Could anyone advise if this methodology is correct? In my results, I am not getting the results that I expect. So there could potentially be a problem with my logic.

  • 1
    Please if you make the effort to modernize some code, use some indentation, otherwise I don't see the point bothering with the translation. – Vladimir F Героям слава Jul 24 '15 at 18:52
  • 1
    It's also worth noting: it's already F90 code. – francescalus Jul 24 '15 at 18:53
  • To make sure that there is nothing missing, you should mention the statements that modify `a` or `b` to make the `if` statement eventually get `.true.`; You can insert just something like `do some computations` in your sample code – innoSPG Jul 24 '15 at 18:58

1 Answers1

6

You got the translation slightly wrong... The first step is to rebuild the do loop, which loops at 15:

Subroutine area(x,y,z,d)
do j=1,10
  if (a.gt.b) go to 20
enddo
20 Statement 1
   Statement 2
   Statement 3
end subroutine

Now you can see that the goto results in "jumping out of the loop". In this particular example, this equivalent to an exit, and the code can be written as

Subroutine area(x,y,z,d)
  do j=1,10
    if (a.gt.b) exit
  enddo
  Statement 1
  Statement 2
  Statement 3
end subroutine
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Thanks Vogt, let me try this. However, I am wondering that there is no 'else' condition to that 'if' statement. What would happen if the statement is false? – Nerdy business Jul 24 '15 at 21:19
  • 1
    If the statement evaluates to FALSE, the loop will continue with its next iteration, until it reaches its limit (in this case 10 iterations). – chw21 Jul 25 '15 at 00:36