Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This has been the way that Fortran loops work for decades and you can't simply change this with a compiler option. The Fortran standard clearly states:</p> <blockquote> <p>8.1.4.4.1 Loop initiation</p> <p>(2) The DO variable becomes defined with the value of the initial parameter <em>m<sub>1</sub></em>.</p> <p>(3) The <strong>iteration count</strong> is established and is the value of the expression <code>MAX (INT ((m2 – m1 + m3) / m3), 0)</code></p> </blockquote> <p>Here <em>m<sub>1</sub></em>, <em>m<sub>2</sub></em> and <em>m<sub>3</sub></em> are the three parameters in the <em>loop-control</em>: <code>[,] var = m1,m2[,m3]</code>, Given your example of <code>i=1,1</code> (<em>m<sub>3</sub></em> is implicitly <code>1</code> if omitted) the iteration count is <code>MAX(INT((1-1+1)/1),0)</code> which evaluates to <code>1</code>, i.e. the loop should get executed once. <code>i</code> is initialised to <code>1</code> as per (2).</p> <blockquote> <p>8.1.4.4.2 The execution cycle</p> <p>The <strong>execution cycle</strong> of a DO construct consists of the following steps performed in sequence repeatedly until termination:</p> <p>(1) The iteration count, if any, is tested. If the iteration count is zero, the loop terminates and the DO construct becomes inactive. If <em>loop-control</em> is <code>[ , ] WHILE (scalar-logical-expr)</code>, the <em>scalar-logicalexpr</em> is evaluated; if the value of this expression is false, the loop terminates and the DO construct becomes inactive. If, as a result, all of the DO constructs sharing the <em>do-term-shared-stmt</em> are inactive, the execution of all of these constructs is complete. However, if some of the DO constructs sharing the <em>do-term-shared-stmt</em> are active, execution continues with step (3) of the execution cycle of the active DO construct whose DO statement was most recently executed.</p> </blockquote> <p>Fortran tests if the remaining <strong>iteration count</strong> is greater than zero, not if the DO variable is less than (greater than) the end value.</p> <blockquote> <p>(2) If the iteration count is nonzero, the range of the loop is executed.</p> <p>(3) The iteration count, if any, is decremented by one. The DO variable, if any, is incremented by the value of the incrementation parameter <em>m<sub>3</sub></em>.</p> </blockquote> <p>The DO variable is always incremented as an iteration of the loop is being executed. Thus after the first execution <code>i</code> becomes incremented by <code>1</code> which evaluates to <code>2</code>.</p> <blockquote> <p>Except for the incrementation of the DO variable that occurs in step (3), the DO variable must neither be redefined nor become undefined while the DO construct is active.</p> <p>8.1.4.4.4 Loop termination</p> <p>When a DO construct becomes inactive, the DO-variable, if any, of the DO construct retains its last defined value.</p> </blockquote> <p>The last defined value is <code>2</code>. Thus after the DO loop has ended, <code>i</code> is equal to <code>2</code>.</p> <p>I've pulled the text out of ISO/IEC 1539:1991 (a.k.a. Fortran 90) but one can also find pretty much the same text in §11.10.3 of ISO/IEC 1539:1980 (a.k.a. ANSI X3J3/90.4 a.k.a. FORTRAN 77; sans the <code>WHILE</code> stuff which is not present in F77) as well as in §8.1.6.6 of ISO/IEC 1539-1:2010 (a.k.a. Fortran 2008).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload