-
Incrementing by 2 not 1
I'm creating a for loop that has to increment by 2 not by 1.
For example something like this.
for ( i = 3; i < sqrt(n); i++ ) "this adding by one"
could i write
for ( i = 3; i < sqrt(n); i2++ )
What is the proper way of writing this so the counter increments by 2
Thanks
Matt
-
-
Code:
for ( i = 3; i < sqrt(n); i+=2 )
-
Thanks man. quick response, very cool.
-
This is valid in C++ (and just about every programming language) too:
i = i + 2; // Increment by 2
Code:
for ( i = 3; i < sqrt(n); (i = i+2) )
But, people will know you're a C++ wimp if you do it that way. :D
And FYI, you can put any expression (anything that evaluates to a number) in the for-statement.
This would be valid C++ too:
Code:
for ( i = 3; i < sqrt(n); (i = (i*2) +5 ) )
-
this is a much more efficient way to increment by 2
i = (i*10)/(2.5*4)+((2^0*2))
-