循环后Fortran DO循环索引的值 [英] Value of Fortran DO loop index after the loop
问题描述
假设您有以下循环:
do i = 1,10
... code ...
end do
write(*,*)I
为什么打印的是11而不是10?
但是当循环由于
而停止时if(something)exit
I正如预期的那样(例如i = 7,因为其他值达到极限而退出)。 i
的值为 在
确定它必须终止。 do
循环之前,11 11
的值是 i
的第一个值,其导致 1
.. 10
失败。因此,当循环完成时, i
的值是 11
。
放入伪代码形式:
1)i < - 1
2)if我> 10 goto 6
3)... code ...
4)i < - i + 1
5)goto 2
6)print i
当它到达第6步时, i
的值为 11
。当你把放入语句中时,它变成:
1) i < - 1
2)if i> 10 goto 7
3)... code ...
4)if i = 7 goto 7
5)i < - i + 1
6)goto 2
7)print i
很清楚 i $ c $在这种情况下,c>将
7
。
How do DO loops work exactly?
Let's say you have the following loop:
do i=1,10
...code...
end do
write(*,*)I
why is the printed I 11, and not 10?
But when the loop stops due to an
if(something) exit
the I is as expected (for example i=7, exit because some other value reached it's limit).
The value of i
goes to 11
before the do
loop determines that it must terminate. The value of 11
is the first value of i
which causes the end condition of 1
..10
to fail. So when the loop is done, the value of i
is 11
.
Put into pseudo-code form:
1) i <- 1
2) if i > 10 goto 6
3) ...code...
4) i <- i + 1
5) goto 2
6) print i
When it gets to step 6, the value of i
is 11
. When you put in your if
statement, it becomes:
1) i <- 1
2) if i > 10 goto 7
3) ...code...
4) if i = 7 goto 7
5) i <- i + 1
6) goto 2
7) print i
So clearly i
will be 7
in this case.
这篇关于循环后Fortran DO循环索引的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!