you could use a variable to increment your counter
for(int counter = 0, increment = 0; counter < 100; increment++, counter += increment){
...do_something...
}
Answer from fabio.ivona on Stack Overflowyou could use a variable to increment your counter
for(int counter = 0, increment = 0; counter < 100; increment++, counter += increment){
...do_something...
}
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...
How do I put two increment statements in a C++ 'for' loop? - Stack Overflow
For loop increment question
[C] Post-increment or pre-increment in for loops?
I need for loop help - C++ Forum
Videos
Hi. As the title state my question is about incrementation when looping. I understand it may sound stupid and for that I apologize.
When you use a nested for loop you write :
for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { Task; } }
My question is when exactly does incrementing happen? Does i get incremented then go into the inner for loop? Does j complete the task then get incremented?
Thank you to anyone who help. I also apologize if I'm not making sense.
A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus:
for(int i = 0; i != 5; ++i,++j)
do_something(i,j);
But is it really a comma operator?
Now having wrote that, a commenter suggested it was actually some special syntactic sugar in the for statement, and not a comma operator at all. I checked that in GCC as follows:
int i=0;
int a=5;
int x=0;
for(i; i<5; x=i++,a++){
printf("i=%d a=%d x=%d\n",i,a,x);
}
I was expecting x to pick up the original value of a, so it should have displayed 5,6,7.. for x. What I got was this
i=0 a=5 x=0
i=1 a=6 x=0
i=2 a=7 x=1
i=3 a=8 x=2
i=4 a=9 x=3
However, if I bracketed the expression to force the parser into really seeing a comma operator, I get this
int main(){
int i=0;
int a=5;
int x=0;
for(i=0; i<5; x=(i++,a++)){
printf("i=%d a=%d x=%d\n",i,a,x);
}
}
i=0 a=5 x=0
i=1 a=6 x=5
i=2 a=7 x=6
i=3 a=8 x=7
i=4 a=9 x=8
Initially I thought that this showed it wasn't behaving as a comma operator at all, but as it turns out, this is simply a precedence issue - the comma operator has the lowest possible precedence, so the expression x=i++,a++ is effectively parsed as (x=i++),a++
Thanks for all the comments, it was an interesting learning experience, and I've been using C for many years!
Try this
for(int i = 0; i != 5; ++i, ++j)
do_something(i,j);
I habitually used post-increments in for loops because the examples C Programming: The Modern Approach used them. E.g:
for(int i = 0, i < size; i++) {
// Some code here
}Somebody here on r/learnprogramming mentioned that s/he uses pre-increments instead because a post-increment creates a temp variable to return the value of i, which might reduce performance, so I started using ++i instead in for loops. But AFAIK the compiler is fairly smart at optimizing code, so I'm not sure if this code will compile to a more optimized binary if I use the pre-increment. Does it really make a difference, or is it merely a style thing?