[英]Javascript nested for loop not working
This is weird, I'm having issues with simple nested for loops in javascript. 这很奇怪,我在使用javascript中的简单嵌套for循环时遇到了问题。
My code is like this: 我的代码是这样的:
var a = 0, b = 2048;
var i = 0, l = 2048;
for(; a < b; a++) {
for(; i < l; i++) {
console.log(a, b, i, l);
}
}
So while, I'm expecting an output like this (0..2047), 2048, (0..2047), 2048
, I'm having this output: 0, 2048, 0..2047, 2048
where the first variable: a
doesn't simply iterate from 0 to 2047. Rephrasing the concept: while the inner loop iterates correctly, the outer one is executed only once at index 0. 因此,虽然我期望这样的输出
(0..2047), 2048, (0..2047), 2048
,但我却得到以下输出: 0, 2048, 0..2047, 2048
其中第一个变量: a
并非简单地从0到2047进行迭代。重新定义一下概念:虽然内部循环正确地进行迭代,但外部循环仅在索引0处执行一次。
I'm sure it's a simple and silly issue, but I can't really spot that.. 我敢肯定这是一个简单而愚蠢的问题,但我无法真正发现这一点。
COMMENT 评论
Thank you all for finding this issue, it's incredible how I couldn't see that. 谢谢大家发现此问题,真是令我惊讶。 I'm accepting simon's answer because it seems cleaner and more elegant to me:
我接受simon的回答,因为它对我来说看起来更干净,更优雅:
for(var i = 0;...)
but just reset it for(var i = 0;...)
中那样重新初始化变量,而只是将其重置 var a = 0, i = 0
and then reset that in every for statement var a = 0, i = 0
,然后在每个for语句中将其重置 Thanks again! 再次感谢!
You didn't reload i
to 0
between a
loops. 您没有在两次循环之间
a
i
重新加载为0
。 Here the fix : 解决方法:
var a = 0, b = 2048;
var i = 0, l = 2048;
for(a = 0; a < b; a++) {
for(i = 0; i < l; i++) {
console.log(a, b, i, l);
}
}
Just initialize i every time before inner iteration: 每次在内部迭代之前初始化我:
var a = 0, b = 2048;
var i, l = 2048;
for(; a < b; a++) {
for(i = 0; i < l; i++) {
console.log(a, b, i, l);
}
}
The reason this is happening is because you never reset i, so the inner loop will only happen during the first iteration on the outer loop. 发生这种情况的原因是因为您从未重置过i,所以内部循环只会在外部循环的第一次迭代期间发生。 Thereafter i will always be greater than l.
此后,我将始终大于l。
Try this: 尝试这个:
var a = 0, b = 2048;
var i = 0, l = 2048;
for(; a < b; a++) {
for(; i < l; i++) {
console.log(a, b, i, l);
}
i = 0;
}
the loop goes like this: 循环如下所示:
for a = 0; 对于a = 0; itarates while i becomes 2048. then for a =1 the i is 2048, thus i not < l and thus not getting into the loop again.
在i变为2048时迭代。然后对于= 1,i为2048,因此我不<1,因此不会再次进入循环。
var a = 0, b = 2048;
for(; a < b; a++) {
var i = 0, l = 2048;
for(; i < l; i++) {
console.log(a, b, i, l);
}
}
I believe this is what you want. 我相信这就是您想要的。
This is a scope problem. Define them inside this way: 这是一个范围问题。 Variables
a
and i
are not defined in the scope of your for loops. 变量
a
和i
未在for循环的范围内定义。 用这种方式定义它们:
var b = 2048,
l = 2048;
for(var a = 0; a < b; a = a + 1) {
for(var i = 0; i < l; i = i + 1) {
console.log(a, b, i, l);
}
}
edit: squint is absolutely correct, JavaScript doesn't use block scope, for which I mistook as the issue. 编辑:斜视是绝对正确的,JavaScript不使用块作用域,对此我误认为是问题。 Although, you should always consider defining the variables that increment inside the declaration of your for loops to eliminate confusion/ambiguity.
虽然,您应该始终考虑定义在for循环的声明内递增的变量,以消除混淆/歧义。
Try one loop 尝试一次循环
var a = 0, b = 2048; var i = 0, l = 2048; for(; a <= b&&i<=l; a++,i++) { console.log(a, b, i, l); }
With two loops it i is never reset and so only get iterated once, hence the result of 0, 2048, 0..2047, 2048
在两个循环中,我永远不会重置,因此仅迭代一次,因此结果为
0, 2048, 0..2047, 2048
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.