简体   繁体   English

Fortran中的DO循环增量问题

[英]DO loop increment problem in Fortran

I have a problem while using a do loop in fortran, 我在fortran中使用do循环时遇到问题,

REAL W,V,X 
DO 50 W = 0.5,5.0,0.5
DO 50 V = 10.0,1000.0,10.0
DO 50 X = 1.0,10,1.0
C=(W*V*X)/1000.0
WRITE(*,*) W,V,X,C
50 CONTINUE
STOP
END 

If I gave this it is showing that only integers needs to be used in do loop, is there any way to give integers in do loop or any other way to do it? 如果我给出这个结果,则表明在do循环中只需要使用整数,是否有任何方法可以在do循环中使用整数或任何其他方式来做到这一点?

Use integers as your looping indices 使用整数作为循环索引

      REAL W,V,X
      INTEGER I,J,K

      DO 50 I = 1,10
        DO 50 J = 1,100
          DO 50 K = 1,10

            W = 0.5 * I
            V = 10.0 * J
            X = 1.0 * K

            C=(W*V*X)/1000.0
            WRITE(*,*) W,V,X,C

50    CONTINUE
      STOP
      END

You should be able to accomplish the same thing by incrementing a real variable by adding your step value, and using an if then to exit the loop. 通过添加步长值并使用if then退出循环来增加实数变量,您应该能够完成相同的任务。 Clunky, but should work. 笨拙,但应该可以。

The last time I programmed in Fortran, I used punch cards and an IBM-360, so I'm not going to pretend I remember the syntax. 上一次我在Fortran中编程时,我使用了打孔卡和IBM-360,所以我不会假装我还记得语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM