如何在Fortran中的do循环中跳过一些迭代 [英] How to skip a few iterations in a do loop in Fortran

查看:480
本文介绍了如何在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

这篇关于如何在Fortran中的do循环中跳过一些迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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