我想预先递增变量的赋值(预递增值大于 1)?

I want to pre-increment an assignment to a variable (pre-increment by value more than 1)?

本文关键字:大于 变量 赋值      更新时间:2023-10-16

我想在 for 循环中预先递增值赋

for (int x=0; x<100; x+=increase){
// loop operation here
}

上面的代码 post 增加了值,但我想预先递增它。我知道我可以使用++i语法预递增 1,但是有没有办法通过变量赋值预递增。

听起来你有两个误解。

  1. x += y已经"等价"于预递增,因为根据定义,x += 1++x相同。 (关键是x += y的值是 x 的更新值,就像++x的值是更新的值一样。它是后增量表单x++,除了 1 之外,没有添加增量的确切等效项。
  2. 当你写for(x = 0; x < 100; x += increase)时,你不会立即使用表达式x += increase的值,所以你使用前增量还是后增量形式并不重要。

如果您希望循环以increase而不是 0 的初始值开头,只需编写

for(x = increase; x < 100; x += increase)