Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran do loop with internal goto

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"?

like image 842
yu_ominae Avatar asked Jan 05 '12 06:01

yu_ominae


People also ask

Do continue loop in FORTRAN?

The CONTINUE statement is often used as a place to hang a statement label, usually it is the end of a DO loop. The CONTINUE statement is used primarily as a convenient point for placing a statement label, particularly as the terminal statement in a DO loop. Execution of a CONTINUE statement has no effect.

What is nested do loop in FORTRAN?

Just like an IF-THEN-ELSE-END IF can contain another IF-THEN-ELSE-END IF (see nested IF for the details), a DO-loop can contain other DO-loops in its body. The body of the contained DO-loop, usually referred to as the nested DO-loop, must be completely inside the containing DO-loop.

Do loops Fortran 77?

Fortran 77 has only one loop construct, called the do-loop. The do-loop corresponds to what is known as a for-loop in other languages. Other loop constructs have to be built using the if and goto statements.


1 Answers

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.

like image 90
milancurcic Avatar answered Oct 08 '22 20:10

milancurcic