Fortran do loop with internal goto
Asked Answered
B

1

12

I have a Fortran77 snippet that looks like this:

    DO 1301 N=NMLK-2,2,-1                                                     
       Some code...
       IF(NB1(N).EQ.50) GOTO 1300                                                            
       Some code...
       IF(BS(N).EQ.0.0) GOTO 1301                                                
       some code...                                                               
       GOTO 1301                                                                 
  1300 NW(M)=NB1(N)                                                              
       Some code...                                                               
  1301 CONTINUE

When this hits the GOTO 1301 statement, does this jump to the next iteration of the loop or does it exit the loop? As far as I understand the return keyword does nothing, so I assume that this will just exit the loop and continue code execution from label 1301, is that correct?

I am translating this to C# and am wondering if this is equivalent:

for (N = NMLK; N >= 2; N--)
{
    Some code...
    if (NB1[N] == 50)
        goto l1300;
    Some code...
    if (BS[N] == 0)
        return;
    Some code...
    return;
l1300:
    NW[M] = NB1[N];
    Some code...
}

or if I should have "continue" instead of "return"?

Blooded answered 5/1, 2012 at 6:27 Comment(0)
P
12

Yes, the GOTO 1301 statement makes the program jump to next iteration.

The DO label, label CONTINUE is an obsolete way to write a more contemporary DO ENDDO block. In this case the loop will iterate over variables specified on the DO line, and label CONTINUE line serves as an "ENDDO" placeholder.

Praemunire answered 5/1, 2012 at 6:34 Comment(2)
Thanks. Just to be absolutely clear on this, the GOTO 1301 statements will actually continue execution of the loop until the condition for termination of the loop is satisfied?Blooded
Yes. Your GOTO 1301 takes you to 1301 CONTINUE, which will reiterate until the termination of the loop.Praemunire

© 2022 - 2024 — McMap. All rights reserved.