Javascript 是否有循环“重新开始”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8766910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:25:09  来源:igfitidea点击:

Is there a loop "start-over"?

javascriptloops

提问by ajax333221

  • There is continue;to stop the loop and move to the nextloop
  • There is break;to stop the loop and move to the endof the loop
  • continue;停止循环并移动到下一个循环
  • break;停止循环,并移动到结束循环

Isn't there some kind of start;that stop the loop and move to the beginningof the loop?

不是有某种start;停止循环并移动到循环开头的吗?

I know it is easy to achieve all of these three actions by just modifying the value of i, but I always try to look for already built-it functions.

我知道通过修改 的值很容易实现所有这三个操作i,但我总是尝试寻找已经内置的函数。

采纳答案by nnnnnn

No - there is no keyword or other way to do it automatically.

否 - 没有关键字或其他方式可以自动执行此操作。

As you already mentioned you can just modify the loop condition variable(s) within your loop. Easy if it's a simple icounter, but of course you may have more initialisation to do than just a simple counter.

正如您已经提到的,您可以在循环中修改循环条件变量。如果它是一个简单的i计数器,则很容易,但是当然,您可能需要进行更多的初始化,而不仅仅是一个简单的计数器。

Or you can do something like the following:

或者您可以执行以下操作:

restartLoop:
while (true) {
   for (var i=0, j=100000, x="test"; i < 1000; i++, j--, x+= ".") {
      if (/*some condition, want to restart the loop*/)
          continue restartLoop;
   }
   break;
}

The continue restartLoopwill jump back out to continue with the next iteration of the whileloop, which then immediately starts the forloop from the beginning including all of the initialisation code. If the forexits normally the breakstatement after it will break out of the containing while loop.

continue restartLoop会跳回了继续与下一次迭代while循环,然后立即开始for从一开始就包括所有的初始化代码的循环。如果for正常退出,break后面的语句将跳出包含的 while 循环。

I don't really recommend doing this in a general sense, but if your loop initialisation process was reallycomplicated it could be worth it because then you wouldn't need to repeat it all inside the loop. If you needed to do even more initialisation than fits nicely in the forstatement's initialisation expression you can easily put it just before the forloop inside the whileand it will all be re-run...

我真的不建议在一般意义上这样做,但是如果您的循环初始化过程真的很复杂,那可能是值得的,因为这样您就不需要在循环内重复所有这些。如果你需要做更多的初始化而不适合for语句的初始化表达式,你可以很容易地把它放在for里面的循环之前while,它会全部重新运行......

回答by Niet the Dark Absol

Resetting the value of your loop variable to the initial value then calling continueis as close as you'll get.

将循环变量的值重置为初始值,然后调用continue就可以了。

For example:

例如:

for(var i=0; i<20; i++) {
  if(somecondition) {
    i=-1; continue;
  }
}

回答by jfriend00

If you want to avoid jumps or the equivalent of goto statements that many of us have been trained to avoid, you could use a local function for the loop and a test on the return value to see if you should just call it again:

如果你想避免跳转或我们很多人都被训练避免的 goto 语句等价物,你可以使用循环的本地函数和对返回值的测试,看看你是否应该再次调用它:

function doItAll() {
    // put state variables other than the actual loop control here

    function doTheLoop() {
        for(var i=0; i<20; i++) {
            if (somecondition) {
                return(true);    // run the loop again
            }
        }
        return(false);   // done running the loop
    }
    while (doTheLoop()) {}
    // do some things after the loop
}

回答by ziesemer

No. (Just to rule out a "I just haven't heard of it, either" - it isn't mentioned at https://developer.mozilla.org/en/JavaScript/Reference/Statements.)

不。(只是为了排除“我也没有听说过它”——https: //developer.mozilla.org/en/JavaScript/Reference/Statements 中没有提到。)

回答by ajd

continueworks by simply skipping the rest of the loop body. breakworks by skipping the rest of the loop body and then ending the loop. A startfunction would have to somehow "rewind" the state of the program - but not all of the state of the program, since presumably you don't want to lose what you did, either - to where it was when the loop began, which is not something that any programming language that I have seen provides.

continue通过简单地跳过循环体的其余部分来工作。break通过跳过循环体的其余部分然后结束循环来工作。一个start函数必须以某种方式“倒带”程序的状态 - 但不是程序的所有状态,因为大概你也不想丢失你所做的 - 到循环开始时的位置,即不是我见过的任何编程语言都提供的东西。

回答by user1135379

You could have the loop in a function that calls itself recursively:

您可以在递归调用自身的函数中使用循环:

function loopIt(numTimes) {
            if (numTimes < 3) {
                for (x = 0; x < 20; x++) {
                    if (x == 5) {
                        loopIt(numTimes+1);
                        break;
                    }
                }
            }     
        }

You can obviously change the conditions to fit you logic as the above is a simple example.

您显然可以更改条件以适合您的逻辑,因为上面是一个简单的示例。