loop question

loop question

Hi,

please see the following test program.

     program counter
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    implicit none
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    integer (kind = 4)     :: icel,ncc
! initiate
    ncc = 10
! loop
    do icel = 1,ncc
        write(*,*)icel,ncc
    enddo ! icel
! 
    write(*,*)'================================='
    write(*,*)icel,ncc
    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!

Eide

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
http://software.intel.com/sites/products/documentation/doclib/stdxe/2013...

or see section 8.1.6.6.2-1(3) 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.

Login to leave a comment.