# Incrementing by 2 not 1

• 10-14-2004
Eckey
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
• 10-14-2004
Codeplug
i+=2

gg
• 10-14-2004
PJYelton
Code:

`for ( i = 3; i < sqrt(n); i+=2 )`
• 10-14-2004
Eckey
Thanks man. quick response, very cool.
• 10-14-2004
DougDbug
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 ) )`
• 10-14-2004
misplaced
this is a much more efficient way to increment by 2

i = (i*10)/(2.5*4)+((2^0*2))
• 10-14-2004
misplaced
.....nah, just kidding