如何退出“重复直到循环”在fortran? [英] How to exit "repeat until loop" in fortran?

查看:500
本文介绍了如何退出“重复直到循环”在fortran?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了两个二维数组 h hh 。我想分配 hh带有新值的。对于一个特定的 k',我想要 hh(k',j)= 1 ,如果条件

I have defined two 2D arrays h and hh.I want to assign hh with new values. For a specific k', I want hh(k',j)=1, if the condition

h(k',j)>0

是真的;并且一旦条件为假,即 h(k',j')<0 ,那么对于任何 j> j' hh(k',j)= 0 。我使用了下面的 DO WHILE 循环:

is true; and once the condition is false, i.e., h(k',j')<0, then for any j>j', hh(k',j)=0. I used the following DO WHILE loop:

  do k=1, npair
    do j =1, movie
      hh(k,j)=0.0
    enddo
  enddo

  do k=1, npair
    do j =1, nmovie
       do while (h(k,j)>0)
         hh(k,j)=h(k,j)
       enddo
    enddo

但是如果条件(h(k,j)> 0 )永远是真的,会有一个无限循环!您能否建议如何实现它?

But if the condition (h(k,j)>0) is always true, there will be a infinite loop! Could you please suggest how can implement it?

推荐答案

在我看来,您可以设置每个值 hh 给定 h 的值。我还假设 hh h 是相同的大小。所以你应该为hh中的每个元素做东西。我建议如下:

It seems to me that you can set each value of hh given the value of h. I'm also assuming hh and h are the same size. So you should do something for each element in hh. I recommend the following:

do k=1,N1    ! N1 and N2 are the limits of the hh and h array.
   do j=1,N2
      if ( h(k,j) > 0) then    ! Check the condition for a specific element in h
         hh(k,j) = 1
      else
         ! -- We need to set *all* values in the desired range
         hh(k,j:N2) = 0

         ! -- And we need to stop loop from overwriting values hh(k,j+1), for example
         ! -- So we break out of the j loop
         exit
      endif
   enddo
enddo

您应该检查以确保它符合您的想法。请注意,我使用冒号表示法在hh数组中指定一个值范围。
此外,如果 h(k,j)为0,您将不清楚发生了什么。

You should check to make sure this does what you think it will. Note that I'm using colon notation to assign a range of values in the hh array. Also, you're unclear on what happens if h(k,j) is 0 exactly.

这篇关于如何退出“重复直到循环”在fortran?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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