0

I am converting a cpp prog (from another author) to a Fortran prog, my C is not too strong. I came across for-loop constructs starting with

for (int n = 1; 1; ++n) {
...

I would have expected this to convert to a Fortran Do as per

Do n=1, 1, 2
...

... at least that is my guess based on my understanding of what ++n will do.

Is my translation correct? If so, the loop will cycle at most once, so what am I missing ???

I understand that in some ways c for-loops have a "do-while" aspect, and hence wrinkles porting to Fortran Do's.

Anyway ... a clarification would be much appreciated.

EDITED: after some prompt responses, and I think I see where this is going

First, the exact C code copy/paste but "trimming" a little, is

    for (int n = 1; 1; ++n) {
      const double coef = exp(-a2*(n*n)) * expx2 / (a2*(n*n) + y*y);
      prod2ax *= exp2ax;
      prodm2ax *= expm2ax;
      sum1 += coef;
      sum2 += coef * prodm2ax;
      sum4 += (coef * prodm2ax) * (a*n);
      sum3 += coef * prod2ax;
      sum5 += (coef * prod2ax) * (a*n);
      // test convergence via sum5, since this sum has the slowest decay
      if ((coef * prod2ax) * (a*n) < relerr * sum5) break;
    }

So yes, there is a "break" in the loop, which on the Fortran side is replaced with an "Exit".

I think the key seems to be from the answers below that the original code's author created the for (int n=1; 1 ; ++n ) precisely to create a an infinite loop, and I had not guessed that this for construct would create an infinite loop.

Anyway, I can certainly create an infinite loop with an "Exit" in Fortran (though I expect I might "do" it a bit more judiciously)

Many thanks to all.

It seems the Mr Gregory's response was the one that imediately lead to a solution for me, so I will mark his correct. As for the Fortran side, there are a number of alternatives such as:

  Do While
    :
    If( something ) Exit
  End Do

but being old fashioned I would probably use a construct with a "limit" such as

   Do i=1, MaxIter
    :
    If( something ) Exit
   End Do

For slightly fancier applications I might include a return flag in case it did not converge in MaxIter's etc.

1
  • Which Fortran? Fortran77, Fortran95, Fortran2005? And which C++? C++11? BTW, why do you convert it? Calling C++ code from Fortran might be simpler than converting it! Commented Sep 18, 2017 at 6:25

5 Answers 5

2

It's difficult to be definitive without seeing how the C++ program breaks out of that loop, but a straightforward Fortran equivalent would be

n = 1
do
    ! code, including an exit under some condition, presumably on the value of n
    n = n+1
end do

If the loop is terminated when n reaches a critical value then the equivalent might be

do n = 1, critical_value  ! no need to indicate step size if it is 1
    ! code
end do
Sign up to request clarification or add additional context in comments.

Comments

1

Are you sure you wrote the C code correctly? Typically loops in C/C++ are done like this:

for (int n = 1; n < 10; ++n) {
    // ...
}

Note the "n < 10" test condition. Your code's test condition is simply 1, which will always evaluate to Boolean "true". This means the code will loop infinitely, unless there's a break inside the loop, which you haven't shown.

++n means "increment n".

So if the code you've shown is indeed correct, the FORTRAN equivalent would be:

n = 1
do
    [Body of the loop, which you haven't shown]
    n = n + 1
enddo

2 Comments

Okay, then do while ( .TRUE. )?
Point taken, and I've edited my response. The larger picture here is not so much how to do a FOR loop in FORTRAN, but that the C loop shown by the OP is not a typical C for() loop.
0

Here's what

for (int n = 1; 1; ++n)

does:

It sets n to 1, then loops infinitely, incrementing n by 1 at the end of each loop iteration. The loop will never terminate unless something inside the loop breaks out.

It's been a long time since I wrote Fortran but as I recall the do loop you translated it to is not correct.

Comments

0

I don't think you can translate

for (int n = 1; 1; ++n)

to a FORTRAN DO loop. From what I recall, the notion of the generic conditional in C/C++ cannot be emulated in a FORTRAN DO loop.

The equivalent of

Do n=1, 1, 2

in C/C++ is

for ( int n = 1; n <= 1; n += 2 ) 

Comments

0

A few notes in addition to CareyGregory’s answer.

++n means ‘increment n by one (before n is evaluated)’

In C and C++, a for loop has three clauses, much like in FORTRAN:

for (init; condition; increment)

The difference is that each of the clauses must be a complete expression, whereas in FORTRAN the clauses are just values. It is just a ‘short’ way of writing an equivalent while loop:

int n = 1;          │ for (int n = 1; 1; ++n)    │ n = 1
while (1)           │ {                          │ do 
{                   │   ...                      │   ...
  ...               │ }                          │   n = n + 1
  ++n;              │                            │ enddo
}                   │                            │

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.