开发者

How do `DO` loops work in Fortran 66?

开发者 https://www.devze.com 2023-03-23 16:10 出处:网络
I\'m reading an old book I found in a second-hand book shop (again). This one is called \"Fortran techniques - with special reference to non-numerical applications\", by A. Colin Day, published by Cam

I'm reading an old book I found in a second-hand book shop (again). This one is called "Fortran techniques - with special reference to non-numerical applications", by A. Colin Day, published by Cambridge University Press in 1972. It is, after all, very important to keep up with the latest in software development ;-)

This book claims to cover Fortran-66 (X3.9-1966), aka Fortran-IV, with a minor departure from that standard for DATA statements which isn't relevant here.

The trouble is, the book seems to leave a lot to guesswork, and my guesses are pretty uncertain WRT the DO loop. This is in chapter 1, so not a very good sign.

Here is one example...

    DO 15 I = 1, 87
    J = I - 44

In the DO line, 1 and 87 seem to represent the inclusive range for the loop - I takes values 1 to 87 inclusive, so J takes values -43 to +43 inclusive. However, what does the 15 represent?

Another example is...

    N = 1
    DO 33 I = 1, 10
    ...
33  N = N + N

In this case, 33 looks like a label or line number - presumably the last line executed before the loop repeats (or exits). But 33 is an odd number to choose just as an arbitrary label.

开发者_如何学JAVA

EDIT That was a mistake - see the answer by duffymo - How do `DO` loops work in Fortran 66?

And the very next example after that is...

    DO 33 I = 1, 10
    N = 2 ** (I-1)

Again using the same 33, but without any line being explicitly labelled with it.

Am I being confused because these are short snippets taken out of context? What does the n in DO n ... represent?


Here is a complete program that should answer some of your questions. One can easily test this history question ... FORTRAN IV is still supported by numerous compilers, though portions of FORTRAN IV are either officially obsolescent or, in my opinion, should be obsolete. I compiled and checked this program with both g77 (which is close to obsolete since it is long unsupported) and gfortran.

Here is a sample program:

      implicit none

      integer i
      real q

      q = 1.0
      do i=1, 10
         q = q * 1.5
      end do
      write (6, *) "modern loop: q =", q

      q = 1.0
      do 100 i=1, 10
         q = q * 1.5
  100 continue
      write (6, *) "loop with continue: q =", q

      q = 1.0
      do 200 i=1, 10
  200 q = q * 1.5
      write (6, *) "loop without continue: q =", q

      stop
      end

And how to compile it with gfortran: gfortran -ffixed-form -ffixed-line-length-none -std=gnu test_loops.for -o test_loops.exe

Re your question: if you terminate the loop with a labeled line that is an executable code, is that line part of the loop? The output of the program clearly shows that the labeled line IS part of the loop. Here is the output of gfortran:

modern loop: q = 57.665039
loop with continue: q = 57.665039
loop without continue: q = 57.665039


The line number tells the code where to go when the loop is complete.

Yes, the numbers are odd, arbitrary, and meaningless. It's part of what made FORTRAN hard to read and understand.


The number 15 known as a "Label" it was decided by the programmer. Depending on organisational standards these numbers were controlled and followed specific rules. Although some programmers didn't keep to standards and their code was a mess; Comments and line indentations were also part of standards followed by most.

0

精彩评论

暂无评论...
验证码 换一张
取 消