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 carla
I would expect L=LM=24… And SOMA=40…
But instead I get:
L = 25
SOMA = 41
I 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.
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.