简体   繁体   English

我如何在不破坏javascript中主要for循环的情况下中断sub for循环?

[英]How do I a break a sub for loop without breaking the main for loop in javascript?

I have a for loop and inside that for loop I have another for loop with a condition for break. 我有一个for循环,在该for循环内,我还有一个带有break条件的for循环。 The problem is that when the sub for loop breaks the top-level loop also does it. 问题在于,当sub for循环中断时,顶级循环也会执行该操作。 So how do I a break a sub for loop without breaking the main for loop in javascript? 那么,如何在不破坏javascript中主要的for循环的情况下中断sub for循环呢?

Use a break statement, thus: 使用break语句,因此:

var i, j, s = "";

for(i = 0; i < 5; ++i) {
    s += "\ni = " + i;
    for(j = 0; j < 999; ++j) {
        s += "\nj = " + j;
        if(j === 1) {
            break;
        }
    }    
}

Notice that at the end, s contains the result of five iterations of the whole loop, rather than one. 请注意,最后, s包含整个循环的五次迭代的结果,而不是一次。

One possible cause for your issue might be an extra misplaced semicolon, thus: 您问题的可能原因可能是分号放错了,因此:

var i, j, s;
s = "";
for(i = 0; i < 5; ++i) {
    s += "\ni = " + i;
    for(j = 0; j < 10; ++j); {
        s += "\nj = " + j;
        if(j === 10) {
            break;
        }
    }    
}

In the above case, the inner for loop is actually empty, but indentation makes it look as though it contains the following block (which is actually an immediate child of the "outer" loop). 在上述情况下,内部for循环实际上为空,但是缩进使它看起来好像包含以下块(实际上是“外部”循环的直接子代)。 This means that the apparent "outer" loop will break on the first iteration. 这意味着明显的“外部”循环将在第一次迭代时中断。

Another cause might be a typo leading to the incorrect use of assignment vs equality, thus: 另一个原因可能是拼写错误,导致错误使用赋值与相等,因此:

var i, j, s;
s = "";
for(i = 0; i < 5; ++i) {
    s += "\ni = " + i;
    for(j = 0; j < 10; ++j) {
        s += "\nj = " + j;
        if(i = 5) {
            break;
        }
    }    
}

In this situation, in the condition that causes the inner loop to break, the loop variable of the outer loop has been assigned a value that causes the outer loop to finish. 在这种情况下,在导致内部循环中断的情况下,已为外部循环的循环变量分配了一个导致外部循环结束的值。 Assignment is truthy, so the break will always be hit. 分配是真实的,所以总是会遇到突破。

Yet another cause might be a misunderstanding about variable scope in javascript. 另一个原因可能是对javascript中变量范围的误解。 In other languages, you might be able to do something like this: 在其他语言中,您可能可以执行以下操作:

var i, s = "";
for(i = 0; i < 5; ++i) {
    s += "\nouter = " + i;
    if(true) {
        var i;
        for(i = 0; i < 999; ++i) {
            s += "\ninner = " + i;
            if(i === 10) {
                break;
            }
        }
    }    
}

The trouble here is that in javascript, variables are scoped to the function in which they are declared with var . 这里的麻烦在于,在javascript中,变量的作用域范围是使用var声明变量的函数。 (or global, if outside a function or used without var ). (或全局(如果在函数外部或不带var ))。 This means that the i of the inner loop is actually the same i as the outer loop 这意味着内部循环的i实际上与外部循环的i相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM