当内循环中的条件变为真时,打破外循环 [英] Breaking Outer loop when Condition become true in Inner loop

查看:202
本文介绍了当内循环中的条件变为真时,打破外循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

假设我们有如下的嵌套循环....

hello friends,

suppose we have nested loops as below....

for (int y = 0; y < someLength; y++)
            {
                for (int x = 0; x <somelength;x++)          
                 {
                    if (condition becomes true)
                    {
                       Break both loops inner loop and outer loop.......
                    }
                }
            }


有什么标准的方法可以打破两个循环..........当条件仅在内部循环中变为真时

如我们所知....可以打破我们所处的循环...使用break ...

谢谢........


is there any standard way to break both loops..........when condition becomes true in just Inner loop

as we know.... can break loop in which we are nicely...... using break...

Thanks........

推荐答案

我唯一能想到的就是将嵌套循环放入自己的方法中,如果合适的条件是满足,只需从方法中返回即可.

The only thing I can think of is to put the nested loops into their own method, and if the appropriate condition is met, simply return from the method.

private void LooperMethod()
{
    for (int i = 0; i < somevalue; i++)
    {
        for (int j = 0; j < someOtherValue; j++)
        {
            if (conditionExists)
            {
                if (specialConditionExists)
                {
                    return;
                }
                break;
            }
            if (outerConditionExists)
            {
                break;
            }
        }
    }
}


如果您无法将循环代码抽象为自己的方法,那么唯一的选择就是维护一个变量,该变量指示何时中断外部循环:


If you can''t abstract the loop code into its own method, your only real choice is to maintain a variable that indicates when to break the outer loop:

bool breakOuter = false;
for (int i = 0; i < somevalue; i++)
{
    for (int j = 0; j < someOtherValue; j++)
    {
        if (conditionExists)
        {
            if (specialConditionExists)
            {
                breakOuter = true;
            }
            break;
        }
        if (outerConditionExists || breakOuter)
        {
            break;
        }
    }
}


以下代码不会返回,但会退出循环,并在循环后执行下一个代码

below code will not return but come out of loop and will execute next code after loops

bool loopbreak=new bool()  ;
           loopbreak = false;
           for (int y = 0; y < 10 && loopbreak==false; y++)
           {
               for (int x = 0; x <10;x++)
                {
                   if (x==5)
                   {
                      loopbreak=true;
                      break;
                   }

               }
          }


尝试此代码
try this code
for (int i = 0; i < 5; i++)
           {
               for (int j = 0; j < 5; j++)
               {

                   if (i == 1)
                   {
                       return;
                   }
                   MessageBox.Show("j" + j.ToString());
               }
               MessageBox.Show("i" +i.ToString());
           }


这篇关于当内循环中的条件变为真时,打破外循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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