0

Bad news Everyone,

I recently started to learn FORTRAN to understand a code and translate it to C++ (I
know what the code does is not important for this question, but to give some background,
this code solves boundary value problems using collocation method on an adaptive grid).
Looking at the code, there are certain "go to" statements combined with "do continue"
that really confuse me.

Here is an example:

do 100 i=1, n
    C DO SOMETHING HERE
    if (some condition) go to 90
    C DO SOME MORE STUFF HERE
    90 continue
    C EVEN MORE STUFF HERE
100 continue

I have some experience programming in C, so I'll code my two interpretations in C as
follows, but I'm not sure which one (if any!) is the correct translation:

for(int i=1;i<=n;i++)
{
    //DO SOMETHING HERE
    if(some condition) continue;
    //DO SOME MORE STUFF HERE
    //EVEN MORE STUFF HERE
}

or:

for(int i=1;i<=n;i++)
{
    //DO SOMETHING HERE
    if(!some condition)
    {
        //DO SOME MORE STUFF HERE
    }
    //EVEN MORE STUFF HERE
}

I was wondering if anyone can help me understand the syntax.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
triple_r
  • 1,037
  • 1
  • 8
  • 21

1 Answers1

5

The continue statement of Fortran does nothing. It is only there as a reference to jump to. In your Fortran code, the program jumps over DO SOME MORE STUFF HERE if some condition is true. Thus, your 2nd C version is correct.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • Thank you. So is it just a style preference to use "if-go to" instead of "if .not.-endif"? – triple_r Feb 07 '13 at 05:33
  • It has the same effect, but I would not call it a style issue: The problem with the "GOTO some label" statement is that code with labels is hard to maintain, since your program can jump to a statement with a label from anywhere within the program, which allows you to write totally unstructured code, see Dijkstra's letter "Go To Statement Considered Harmful" – Reinhard Männer Feb 07 '13 at 06:18
  • 1
    http://web.archive.org/web/20090320002214/http://www.ecn.purdue.edu/ParaMount/papers/rubin87goto.pdf http://kerneltrap.org/node/553/2131 ... representing the other side. GOTO is neither good nor bad. It's just a statement. – Rook Feb 07 '13 at 17:16
  • @ldigas spoken as someon who's not had the pleasure of debugging someone elses code littered with gotos. For historical perspective early (Pre 77) fortran did not have the if then construct. The if-goto form is indicative of very old code or at least code written by someone older than me.. – agentp Feb 07 '13 at 21:44