如何在Fortran中的do循环中跳过一些迭代

问题描述:

例如,我想以1为增量从1循环到500.但是,对于每8个循环,我都希望跳过接下来的18个循环(将do变量增加18).我该怎么办?

For example, I want to loop from 1 to 500 at an increment of 2. However, for every 8 loops I want to skip the next 18 loops (make the do-variable increase by 18). How do I do that?

我的代码是:

event = 0
do i = 1,500,2
    event = event + 1
    if (event .eq. 8) then
          i = i + 18
          event = 0
    endif
enddo

但是,我得到了一个错误:"DO主体中的do变量不应出现在变量定义上下文中".基本上,我无法在循环中更改变量"i".那么我应该如何编写代码来实现它呢?

However, I got the error: "A do-variable within a DO body shall not appear in a variable definition context". Basically I can't alter the variable "i" in the loop. So how should I write the code to realize it?

谢谢.

禁止修改循环索引.您可以通过多种方式解决您的问题.例如,这是一个没有显式循环索引的解决方案:

It is forbidden to modify the loop index. You can solve your problem in several ways. For instance, here is a solution without explicit loop index :

i = -1
do
    i=i+2
    if(i > 5000) exit
    if (i == 15) i=i+18
    ...
enddo