循环后 Fortran DO 循环索引的值 [英] Value of Fortran DO loop index after the loop

查看:12
本文介绍了循环后 Fortran DO 循环索引的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DO 循环是如何工作的?

How do DO loops work exactly?

假设您有以下循环:

do i=1,10
...code...
end do

write(*,*)I

为什么打印的 I 是 11,而不是 10?

why is the printed I 11, and not 10?

但是当循环由于一个

if(something) exit

I 符合预期(例如 i=7,退出,因为其他一些值达到了它的限制).

the I is as expected (for example i=7, exit because some other value reached it's limit).

推荐答案

i 的值在 do 循环确定之前进入 11它必须终止.11 的值是 i 的第一个值,它导致 1..10 的结束条件失败.所以当循环完成后,i的值为11.

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.

输入伪代码形式:

1) i <- 1
2) if i > 10 goto 6
3) ...code...
4) i <- i + 1
5) goto 2
6) print i

到第6步时,i的值为11.当您放入 if 语句时,它变为:

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

很明显,在这种情况下,i 将是 7.

So clearly i will be 7 in this case.

这篇关于循环后 Fortran DO 循环索引的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆