Fortran 66中的“DO”循环如何工作? [英] How do `DO` loops work in Fortran 66?

查看:241
本文介绍了Fortran 66中的“DO”循环如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本我在二手书店找到的旧书(再次)。这个被称为Fortran技术 - 特别涉及非数值应用,1972年由剑桥大学出版社出版的A. Colin Day。毕竟,跟上最新的软件开发是非常重要的; - )



本书声称涵盖了Fortran-66(X3.9-1966),又名Fortran-IV,与<$ c $相比略有不同。 c> DATA 这里不相关的语句。

麻烦的是,这本书似乎留下了很多猜测,我的猜测相当不确定WRT DO 循环。这是第1章,所以不是是一个很好的标志。



这是一个例子...

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

DO 行中,1和87似乎表示循环的包含范围 - I 取值1到87(含),所以 J 取值-43到+43。然而, 15 代表什么?



另一个例子是...

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

在这种情况下,33看起来像一个标签或行号 - 大概是循环重复之前执行的最后一行退出)。但33是一个奇数,可以选择为任意标签。



编辑这是一个错误 - 请参阅duffymo - Fortran 66中的DO循环如何工作?



接下来的例子是...

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

再次使用相同的33,但没有明确标记任何行。



我被混淆了,因为这些是简短的片段上下文? DO n ... 中的 n 代表什么?

解决方案

这是一个完整的程序,应该回答你的一些问题。人们可以很容易地测试这个历史问题...... FORTRAN IV仍然受众多编译器的支持,尽管FORTRAN IV的一部分或者正式过时,或者在我看来应该是过时的。我编译和检查了这两个g77(这是接近过时,因为它很长时间不支持)和gfortran这个程序。



下面是一个示例程序:

 隐式无

整数i
实数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继续
写入(6,*)循环:q =,q

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

stop
end

以及如何用gfortran编译它:
gfortran -ffixed-form -ffixed-line-length-none -std = gnu test_loops.for -o test_loops .exe

回答你的问题:如果你用一个可执行代码的带标签的行来终止循环,那么循环的那一行就是这样吗?程序的输出清楚地表明标记的行是循环的一部分。这里是gfortran的输出:

现代循环:q = 57.665039

循环与继续:q = 57.665039

循环没有继续:q = 57.665039

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.

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

这篇关于Fortran 66中的“DO”循环如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆