本文介绍了为什么Fortran DO循环索引大于循环后的上限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 非常简单的例子说明了我的意思: PROGRAM CARLA IMPLICIT NONE ! INTEGER,PARAMETER :: LM = 24,DEZASSEIS = 16 INTEGER :: L,VARIAVEL,SOMA ! DO L = 1,LM WRITE(*,*)'L =',L END DO ! WRITE(*,*)'我现在是DO循环的外部' WRITE(*,*)'我期望L = LM = 24 ...并且SOMA = 40' WRITE(*,*)'L =',L SOMA = DEZASSEIS + L WRITE(*,*)'SOMA =',SOMA 结束程序carla 我预计L = LM = 24 ...而SOMA = 40 ... 但是, get: L = 25 SOMA = 41 我不明白为什么一旦我们在DO循环之外,L就不会保留假设的最后一个值(因此SOMA将等于40),并保持增加... 有人可以给我一个提示吗? 解决方案从1到24循环,所以当它到达25时,循环结束。 (伪代码) LL = 1 当LL LL = LL + 1; 正如GummiV所说,不要这样做。循环是编译器优化的主要目标,不能保证循环执行后的内容。可能同样容易为0,因为在某些机器上检测LL = 0比LL> 24快,所以一次优化可以反转计数。速度并不快,但编译器人员会遇到实际问题。 I am looking inside the code of an air quality model written in fortran, and have some questions regarding the way fortran passes variables out from do-loops.This very simple example illustrates what I mean: PROGRAM carla IMPLICIT NONE ! INTEGER, PARAMETER :: LM = 24, DEZASSEIS = 16 INTEGER :: L, VARIAVEL, SOMA ! DO L=1,LM WRITE(*,*) 'L = ', L END DO ! WRITE(*,*) 'I am now ouside of the DO loop.' WRITE(*,*) 'I would expect L=LM=24... And SOMA=40' WRITE(*,*) 'L = ', L SOMA = DEZASSEIS + L WRITE(*,*) 'SOMA = ', SOMA END PROGRAM carlaI would expect L=LM=24... And SOMA=40...But instead I get: L = 25 SOMA = 41I don't understand why once we are outside of the DO loop, L does not keep the last value assumed (SOMA would be thus equal to 40), and keep increasing...Can somebody give me a hint? 解决方案 Loop from 1 to 24,So when it gets to 25, loop has finished.Think of it as(pseudocode)LL = 1While LL < 25 LL = LL + 1;As GummiV stated, don't do this. Loops are prime targets for compiler optimisations, no guarantee what's in there after the loop has executed. Could have just as easily been 0, one optimisation reverses the count because detecting LL = 0 is quicker than LL > 24 on some machines. Not much quicker, but compiler guys have a real problem with it'll do. 这篇关于为什么Fortran DO循环索引大于循环后的上限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 01:54