Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

loop question

eide
Beginner
631 Views

Hi,

please see the following test program.

[fortran]

     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

[/fortran]

[plain]

screen output

           1          10
           2          10
           3          10
           4          10
           5          10
           6          10
           7          10
           8          10
           9          10
          10          10
 =================================
          11          10

[/plain]

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

0 Kudos
3 Replies
mecej4
Honored Contributor III
631 Views
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/composerxe/compiler/fortran-win/index.htm or see section 8.1.6.6.2-1(3) of the Fortran-2008 Standard.
0 Kudos
TimP
Honored Contributor III
631 Views
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.
0 Kudos
eide
Beginner
631 Views
Thank you for your answers. I was not aware of that before. I stumbled on this while running into a segmentation fault.
0 Kudos
Reply