Javascript for 循环的增量/减量可以超过 1 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12809776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:31:32  来源:igfitidea点击:

Can a for loop increment/decrement by more than one?

javascriptfor-loopincrement

提问by brentonstrine

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

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

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

回答by Andrew Whitaker

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 文章

回答by ninjagecko

A forloop:

一个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.

你可以写几乎所有的表达式INITTESTADVANCE,和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+=1and the like):

请注意,++运算符和变体是具有副作用的运算符(如果您不使用它们,则应尽量避免它们i+=1):

  • ++imeans i+=1; return i
  • i++means oldI=i; i+=1; return oldI
  • ++i方法 i+=1; return i
  • i++方法 oldI=i; i+=1; return oldI

Example:

例子:

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

回答by Adil Malik

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

回答by Paul S.

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 trueor false.

Andrew Whitaker 的回答是正确的,但您可以对任何部分使用任何表达式。
请记住,第二个(中间)表达式应该求值,以便将其与布尔值truefalse.

When I use a forloop, I think of it as

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

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

as being

作为

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

回答by Sudesh Banskota

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 + 3but never assigning the result back to i. i++is just a shorthand for i = i + 1, similarly i +=3is a shorthand for i = i + 3.

你当然可以。其他人已经正确指出你需要做i += 3。您无法执行您发布的操作,因为您在这里所做的只是添加i + 3但从未将结果分配回i. i++只是 的简写i = i + 1,同样i +=3是 的简写i = i + 3

回答by Jayantha

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

回答by Neville Lusimba

The last part of the ternary operator allows you to specify the increment step size. For instance, i++ means increment by 1. i+=2 is same as i=i+2,... etc. Example:

三元运算符的最后一部分允许您指定增量步长。例如,i++ 表示增加 1。i+=2 与 i=i+2 相同,等等。 示例:

let val= [];

for (let i = 0; i < 9; i+=2) {
  val = val + i+",";
}


console.log(val);

Expected results: "2,4,6,8"

预期结果:“2,4,6,8”

'i' can be any floating point or whole number depending on the desired step size.

'i' 可以是任何浮点数或整数,具体取决于所需的步长。