C++ 如何增加 For 循环增量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28519416/
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-28 21:00:12  来源:igfitidea点击:

C++ How do I increase For Loop Increments

c++for-loopincrement

提问by Tsingh321

I want to increase the increment by 1 each time. I want to be able to get 1, 3, 6, 10, 15, 21, 28, 36, 46...

我想每次将增量增加 1。我希望能够得到 1, 3, 6, 10, 15, 21, 28, 36, 46 ...

First it adds 1 then 2 then 3 then 4 and so on and so fourth.

首先它添加 1 然后 2 然后 3 然后 4,依此类推,依此类推。

回答by fabio.ivona

you could use a variable to increment your counter

你可以使用一个变量来增加你的计数器

for(int counter = 0, increment = 0; counter < 100; increment++, counter += increment){
   ...do_something...
}

回答by Kacy

int incrementer = 1;
for ( int i = 1; i < someLength; i += incrementer )
{
    cout << i << endl;
    ++incrementer;
}

or if you want to do it in as few lines as possible (but less readable):

或者,如果您想在尽可能少的行中完成(但可读性较差):

for ( int i = 1, inc = 1; i < 100; ++inc, i += inc )
      cout << i << endl;

Output:

输出:

1

1

3

3

6

6

10

10

etc...

等等...

回答by Sumedh

You can try this:

你可以试试这个:

int value=0;
for(int i=1;i<limit;i++){
    value+=i;
    System.out.println(value);
}

回答by Adan Vivero

I'm assuming that you want the number 45 instead of 46. Therefore I'd say we could use a for loop for this one.

我假设您想要数字 45 而不是 46。因此我想说我们可以使用 for 循环。

int x = 0; 
int y = 1;

for(int i = 0; i <= 9; i++)
{
    x += y;
    cout << x << ", ";
    y++;
}

I stopped at 9 since that's what you used up ahead as the last number. Obviously we can go on longer.

我在 9 处停了下来,因为那是您前面用作最后一个数字的数字。显然,我们可以继续下去。

回答by Swift - Friday Pie

Your question offers one sequence, but incorrect hypotesis.

你的问题提供了一个序列,但不正确的假设。

1, 3, 6, 10, 15, 21, 28, 36, 46 - increment here is 2,3,4,5,6,7,8,10. 10? Should the last value be equal to 45?

1, 3, 6, 10, 15, 21, 28, 36, 46 - 这里的增量是 2,3,4,5,6,7,8,10。10?最后一个值应该等于 45 吗?

In general loop would look like:

一般来说,循环看起来像:

const unsigned begin_count = 1;
const unsigned begin_increment = 2;
for(unsigned count = begin_count, incr = begin_increment; condition; count += incr, ++incr) 
{
}

Where conditionis some kind of expression which must be true as long the loop's body to be executed.

哪里condition是某种表达式,只要循环体被执行,它就必须为真。

Let say, that's actually right and perhaps 46 is the end of array you never want to miss. Or 8 is increment at which you want to stop add one and start add 2, then conditionshould be designed accordingly. You actually can do increment inside of conditionif it required to be done before the body loop is executed! The comma in for()expressions are sequence operators and ternary operator is allowed (Function call, comma and conditional operators). Note, the first "parameter" of for() loop isn't an expression, it's a statement, and meaning of comma there depends on the nature of statement. In this particular case it's a declaration of two variables.

可以说,这实际上是正确的,也许 46 是您永远不想错过的数组的结尾。或者8是你想停止加1并开始加2的增量,那么condition应该相应地设计。condition如果需要在执行主体循环之前完成,您实际上可以在内部进行增量!for()表达式中的逗号是序列运算符,允许使用三元运算符(函数调用、逗号和条件运算符)。注意,for() 循环的第一个“参数”不是表达式,而是语句,逗号的含义取决于语句的性质。在这种特殊情况下,它是两个变量的声明。

At that stage when for() becomes too complex, for readability one should consider use of whileor do whileloops.

在 for() 变得过于复杂的那个阶段,为了可读性,应该考虑使用whileordo while循环。

constexpr unsigned begin_count = 1; 
constexpr unsigned begin_increment = 2; 
constexpr unsigned end_count = 46;  // 45 + 1
for(unsigned count = begin_count, incr = begin_increment; 
    count < end_count;
    count += incr, ++incr ) 
{
}