Logical Operator & amp; & Amp; In a statement

I’m iterating over an array and want to do the following:

for (
    var i = 0;
    i < array.length && !(array[i][1] == 0 && array[i][2] == 'foo');
    i++)
{

So basically I’m saying “if i is less than the array’s length AND it’s not true that array[i]1 is 0 AND array[i][2] is ‘foo'” then do a bunch of stuff.

But it doesn’t work. It always evaluates to false when array[i][2] is ‘foo’, even if array[i][1] is not equal to 0.

Strangely though if I make the for statement like this:

for (var i = 0; i < array.length; i++) {

… and then put this right at the beginning of the loop:

if (array[i][1] == 0 && array[i][2] == 'foo') continue;

… it works. So I must be messing up the syntax somehow when I’m trying to say “IF TRUE AND !(CONDITION 1 && CONDITION 2)” but I’m not sure what I’m doing wrong. Where’s my error?

If you put the test !(array[i][1] == 0 && array[i][2] == 'foo')
in the loop control expression, then when that fails, the loop stops
. It doesn’t skip that iteration and continue to the next one any more than it would if i < array.length
were false. It just stops.

If you want it to continue to the next iteration, you need to put the test in the loop body and use continue
.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Logical Operator & amp&semi; & Amp&semi; In a statement

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录