1

I often see Fortran code functionally similar to the excerpt below. Had I written it, I would have used "K" instead of "J" in the write statement. Using "J" gives the same results, but is that just luck, or is it an expected behavior based on Fortran rules? I came up empty-handed searching for an applicable rule.

K = [arithmetic expression]
do J = 1, K
  X(J) = [some function of J]       
enddo
write(*,*) 'Number of loops: ', J
francescalus
  • 30,576
  • 16
  • 61
  • 96
  • 1
    This - https://stackoverflow.com/questions/11874421/how-can-i-make-the-loop-counter-not-be-greater-than-the-final-value - provides the answer, albeit the question is not immediately a duplicate. See, in particular, the second answer to that question. – High Performance Mark Oct 19 '20 at 15:20
  • Note in particular that the value of `j` printed is not the same as the value of `k`, but in this case is `k+1`. More generally, it could be other values. – francescalus Oct 19 '20 at 16:27
  • @HighPerformanceMark -- Thanks. That is exactly what I was looking for, although it only says "Fortran Standard" and does not specify which version, so I assume that means all of them. "8.1.4.4.4 Loop termination When a DO construct becomes inactive, the DO-variable, if any, of the DO construct retains its last defined value." – F90-Forever Oct 19 '20 at 18:12

0 Answers0