开发者

Two Counts for the for loop?

开发者 https://www.devze.com 2022-12-19 04:42 出处:网络
Can you have two counts with for loop? Example: for( var count1 = 0, count2 = 0; count开发者_如何学C1 < 5; count1++, count2++ ) { }

Can you have two counts with for loop?

Example:

for( var count1 = 0, count2 = 0; count开发者_如何学C1 < 5; count1++, count2++ ) { }

If not, what would be a good way to handle two separate counts other than using two loops?


Yes, of course you can have multiple initializations in a for loop.


Yes, that's valid.


Your loop syntax is completely valid. No problem at all. :)


I don't think you can have multiple counter variables in a for loop. Do you intend the counter variables (count1 and count2) to increment at the same time after each iteration? If so, this is what I'd do:

var count2 = 0;
for (var count1 = 0; count1 < 5; count1++)
{
  // Do stuff
  count2++;
}

Edit: Nevermind. It's legal. My Javascript's rusty and I've never used multiple control variables in a for loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号