loop question

loop question


please see the following test program.

     program counter
    implicit none
    integer (kind = 4)     :: icel,ncc
! initiate
    ncc = 10
! loop
    do icel = 1,ncc
    enddo ! icel
    end program counter

screen output
           1          10
           2          10
           3          10
           4          10
           5          10
           6          10
           7          10
           8          10
           9          10
          10          10
          11          10

Why is icel one more after the loop? I am not new to fortran, but I have never observed that before. I am using using composer_xe_2011_sp1.9.293 on an 64bit opensuse 12.1 with kernel 3.1.10-1.16. If someone could explain that to me, would be fine!


4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Why is icel one more after the loop?
Because that is what the Fortran Standard specifies. See the explanation at

or see section of the Fortran-2008 Standard.

This behavior was defined in the f77 standard. Until 30 years ago, a compiler I used produced 3 different results for the value of the loop counter after loop termination, depending on optimization level.

Thank you for your answers. I was not aware of that before. I stumbled on this while running into a segmentation fault.

Leave a Comment

Please sign in to add a comment. Not a member? Join today