C++ How do I increase For Loop Increments
Asked Answered
S

5

9

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

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

Scowl answered 14/2, 2015 at 19:32 Comment(4)
Post the code you already have...Laurelaureano
If you have i = 1,2,3,4,5,... then the desired sequence can be derived from the formula i*(i+1)/2.Peninsula
Um is there a certain number you want it to stop at?Curagh
and I'm assuming you want 45 as the last number, not 46Curagh
C
8

you could use a variable to increment your counter

for(int counter = 0, increment = 0; counter < 100; increment++, counter += increment){
   ...do_something...
}
Coleville answered 14/2, 2015 at 19:39 Comment(1)
The i < 100 is a mistake, right? Seems like it should be counter < 100. Also, you can replace counter = counter + increment with counter += increment.Daley
M
4
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

3

6

10

etc...

Malacology answered 14/2, 2015 at 19:36 Comment(0)
E
1

You can try this:

int value=0;
for(int i=1;i<limit;i++){
    value+=i;
    System.out.println(value);
}
Eben answered 14/2, 2015 at 19:37 Comment(0)
C
0

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.

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.

Curagh answered 4/4, 2019 at 20:17 Comment(2)
Or you could get rid of y, make i start at 1, and replace every instance of y in the loop with i.Daley
@GumbyTheGreen sure you can do that too, but this also works since I was getting the result that he wanted too.Curagh
L
0

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?

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 condition is some kind of expression which must be true as long the loop's body to be executed.

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 condition should be designed accordingly. You actually can do increment inside of condition if 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.

At that stage when for() becomes too complex, for readability one should consider use of while or do while loops.

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 ) 
{
}
Lydie answered 8/5, 2019 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.