打破内部foreach循环,并继续外foreach循环 [英] Break inner foreach loop and continue outer foreach loop

查看:308
本文介绍了打破内部foreach循环,并继续外foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个嵌套的foreach循环我怎么打破内部循环,并告诉外,继续在这一点上没有做内环低于任何其他code?

 的foreach(项VAR项)
{
  的foreach(在otheritems VAR otheritem)
  {
    如果(!double.TryParse(otheritem))
    {
      //打破内环
      //继续外循环,所以我们永远不会DoStuff()
    }
  }

  DoStuff();
}
 

解决方案

有关如何使用标志?

 的foreach(项VAR项)
{
  布尔标志= FALSE;
  的foreach(在otheritems VAR otheritem)
  {
    如果(!double.TryParse(otheritem))
    {
        标志=真正的;
        打破;
    }
  }
  如果(旗)继续;

  DoStuff();
}
 

If I have a nested foreach loop how do I do break the inner loop and tell the outer to continue at that point without doing any other code below the inner loop?

foreach(var item in items)
{
  foreach(var otheritem in otheritems)
  {
    if (!double.TryParse(otheritem))
    {
      //break inner loop
      //continue outer loop so we never get to DoStuff()
    }
  }

  DoStuff();
}

解决方案

How about using a flag?

foreach(var item in items)
{
  bool flag = false;
  foreach(var otheritem in otheritems)
  {
    if (!double.TryParse(otheritem))
    {
        flag = true;
        break;
    }
  }
  if(flag) continue;

  DoStuff();
}

这篇关于打破内部foreach循环,并继续外foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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