一个for循环可以增加/减少一个以上吗?

时间:2022-07-21 01:58:47

Are there other ways to increment a for loop in Javascript besides i++ and ++i? For example, I want to increment by 3 instead of one.

除了i++ +和++i之外,在Javascript中还有其他方法可以增加for循环吗?例如,我想增加3而不是1。

for (var i = 0; i < myVar.length; i+3) {
   //every three
}

6 个解决方案

#1


156  

Use the += assignment operator:

使用+=赋值操作符:

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

Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.

从技术上讲,您可以在for循环的最终表达式中放置任何您想要的表达式,但它通常用于更新计数器变量。

For more information about each step of the for loop, check out the MDN article.

有关For循环的每个步骤的更多信息,请参阅MDN文章。

#2


8  

A for loop:

一个for循环:

for(INIT; TEST; ADVANCE) {
    BODY
}

Means the following:

意味着以下:

INIT;
while (true) {
    if (!TEST)
        break;
    BODY;
    ADVANCE;
}

You can write almost any expression for INIT, TEST, ADVANCE, and BODY.

您几乎可以为INIT、TEST、ADVANCE和BODY编写任何表达式。

Do note that the ++ operators and variants are operators with side-effects (one should try to avoid them if you are not using them like i+=1 and the like):

请注意,++操作符和变体是具有副作用的操作符(如果您不使用它们,比如i+=1等,那么应该尽量避免使用它们):

  • ++i means i+=1; return i
  • + +我意味着我+ = 1;返回我
  • i++ means oldI=i; i+=1; return oldI
  • 我+ +意味着里地=我;我+ = 1;返回里地

Example:

例子:

> i=0
> [i++, i, ++i, i, i--, i, --i, i]
[0, 1, 2, 2, 2, 1, 0, 0]

#3


7  

   for (var i = 0; i < 10; i=i+2) {
      // code here
   }​

#4


5  

Andrew Whitaker's answer is true, but you can use any expression for any part.
Just remember the second (middle) expression should evaluate so it can be compared to a boolean true or false.

Andrew Whitaker的回答是对的,但是你可以对任何部分使用任何表达式。只需记住第二个(中间)表达式的值,这样就可以将它与布尔值进行比较。

When I use a for loop, I think of it as

当我使用for循环时,我认为它是。

for (var i = 0; i < 10; ++i) {
    /* expression */
}

as being

作为

var i = 0;
while( i < 10 ) {
    /* expression */
    ++i;
}

#5


1  

You certainly can. Others have pointed out correctly that you need to do i += 3. You can't do what you have posted because all you are doing here is adding i + 3 but never assigning the result back to i. i++ is just a shorthand for i = i + 1, similarly i +=3 is a shorthand for i = i + 3.

你当然可以。其他人已经正确地指出,你需要做i += 3。你不能做你已经张贴的东西,因为你在这里所做的只是添加i+ 3,但是永远不要把结果赋给i++ ++只是i = i+ 1的简写,同样,i+ =3是i = i+ 3的简写。

#6


0  

for (var i = 0; i < myVar.length; i+=3) {
   //every three
}

additional

额外的

Operator   Example    Same As
  ++       X ++        x = x + 1
  --       X --        x = x - 1
  +=       x += y      x = x + y
  -=       x -= y      x = x - y
  *=       x *= y      x = x * y
  /=       x /= y      x = x / y
  %=       x %= y      x = x % y

#1


156  

Use the += assignment operator:

使用+=赋值操作符:

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

Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.

从技术上讲,您可以在for循环的最终表达式中放置任何您想要的表达式,但它通常用于更新计数器变量。

For more information about each step of the for loop, check out the MDN article.

有关For循环的每个步骤的更多信息,请参阅MDN文章。

#2


8  

A for loop:

一个for循环:

for(INIT; TEST; ADVANCE) {
    BODY
}

Means the following:

意味着以下:

INIT;
while (true) {
    if (!TEST)
        break;
    BODY;
    ADVANCE;
}

You can write almost any expression for INIT, TEST, ADVANCE, and BODY.

您几乎可以为INIT、TEST、ADVANCE和BODY编写任何表达式。

Do note that the ++ operators and variants are operators with side-effects (one should try to avoid them if you are not using them like i+=1 and the like):

请注意,++操作符和变体是具有副作用的操作符(如果您不使用它们,比如i+=1等,那么应该尽量避免使用它们):

  • ++i means i+=1; return i
  • + +我意味着我+ = 1;返回我
  • i++ means oldI=i; i+=1; return oldI
  • 我+ +意味着里地=我;我+ = 1;返回里地

Example:

例子:

> i=0
> [i++, i, ++i, i, i--, i, --i, i]
[0, 1, 2, 2, 2, 1, 0, 0]

#3


7  

   for (var i = 0; i < 10; i=i+2) {
      // code here
   }​

#4


5  

Andrew Whitaker's answer is true, but you can use any expression for any part.
Just remember the second (middle) expression should evaluate so it can be compared to a boolean true or false.

Andrew Whitaker的回答是对的,但是你可以对任何部分使用任何表达式。只需记住第二个(中间)表达式的值,这样就可以将它与布尔值进行比较。

When I use a for loop, I think of it as

当我使用for循环时,我认为它是。

for (var i = 0; i < 10; ++i) {
    /* expression */
}

as being

作为

var i = 0;
while( i < 10 ) {
    /* expression */
    ++i;
}

#5


1  

You certainly can. Others have pointed out correctly that you need to do i += 3. You can't do what you have posted because all you are doing here is adding i + 3 but never assigning the result back to i. i++ is just a shorthand for i = i + 1, similarly i +=3 is a shorthand for i = i + 3.

你当然可以。其他人已经正确地指出,你需要做i += 3。你不能做你已经张贴的东西,因为你在这里所做的只是添加i+ 3,但是永远不要把结果赋给i++ ++只是i = i+ 1的简写,同样,i+ =3是i = i+ 3的简写。

#6


0  

for (var i = 0; i < myVar.length; i+=3) {
   //every three
}

additional

额外的

Operator   Example    Same As
  ++       X ++        x = x + 1
  --       X --        x = x - 1
  +=       x += y      x = x + y
  -=       x -= y      x = x - y
  *=       x *= y      x = x * y
  /=       x /= y      x = x / y
  %=       x %= y      x = x % y