【问题标题】:Break condition to continue loop中断条件继续循环
【发布时间】:2013-08-02 07:19:36
【问题描述】:

条件完成后我不知道该怎么做,然后继续循环

(对于无法理解的语言 xD 很抱歉)

在代码中

for(var i=0;i<10;i++)
{
   if( // some condition)
      {
        //codee doesn't matter
      }
   else if ( // some condition )
     {
      //And NOW when code is here i want go to next repetition 
     }

}

当我尝试使用 continue 和 break 时出现错误

非法的break语句

return 也很糟糕,因为它不会移动到下一个重复

我希望我写这个好..谢谢!

【问题讨论】:

  • 我在您的代码中没有看到任何可能导致该错误的 breakcontinue 语句。请发布更完整的示例。

标签: javascript loops conditional-statements break


【解决方案1】:
for(var i = 0; i< 10; ++i) {
    if ( /* some condition */) {
        //codee doesn't matter
    }
}

如果你想继续,什么都不做,它会继续。如果您真的想要一个继续条件,您可以执行以下操作:

for(var i = 0; i< 10; ++i) {
    if ( /* some condition */ ) {
        //codee doesn't matter
    }
    else if ( /* other condition */ ) {
        continue;
    }
}

【讨论】:

    【解决方案2】:

    试试这个:

    for(var i = 0; i < 10; i++)
    {
        bool isCheckrequired = // some condition from else if you mentioned;
        if(!isCheckrequired)//Any code which needs to be executed comes under this condition
        {
            if( // some condition)
            {
                //codee doesn't matter
            }
        }
    }
    

    我在这里首先检查是否需要在特定迭代中完成任何事情(通过将您在 ifelse 块中提到的条件移动到顶部)然后如果需要在特定迭代中执行代码然后放入该代码进入 if 块。

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多