'How to loop and increase by 0.01 everytime?

I'm really confused on this code.

Here's what I want it to do: Start with a "v" value of 5, carry out the rest of the functions/calculations, increase the "v" value by 0.01, carry out the functions/calculations, then increase the "v" value by 0.01 again, carry out the functions...do this 500 times or until a "v" value of 10.00 is reached, whichever is easier to code.

Here is my code at the moment:

//start loop over v  
for(iv=5;iv<=500;iv++) {  
    v=0.01*iv;
    //Lots and lots of calculations with v here
}

Here is what I get: I tried setting iv<=10 so it does 10 loops only just so I could test it first before leaving it on all night. It did only 6 loops, starting at v=0.05 and ending at 0.1. So the problem is that a) it didn't run for 10 loops, b) it didn't start at 5.00, it started at 0.05.

Any help would be appreciated.

EDIT: Holy crap, so many answers! I've tried 2 different answers so far, both work! I've been staring at this and changing code around for 3 hours, can't believe it was so easy.



Solution 1:[1]

You need to start at iv = 500. and if you want 10 loops, and iv++ is the update, then you stop before 510.

Reason: v = 0.01*iv, so v = 5 means iv = 5/0.01 = 500. As for the number of iterations, if your for loop is of the form for (x = N; x < M; x++) (constant N and M), then max(0, M-N) loops are executed, if x is not changed in the loop and no weird stuff (e.g. overflow, hidden casts of negative numbers to unsigned, etc.) occurs.

EDIT

Instead of using v = 0.01 * iv, v = iv / 100.0 is probably more accurate. Reason: 0.01 is not exactly representable in floating point, but 100.0 is.

Solution 2:[2]

Changing SiegeX's code so it uses integers ("more accurate"):

double dv;
int iv;
for(iv = 500; dv <= 1000; iv += 1)
{
    dv = (double)iv / 100.0;
}

Solution 3:[3]

double iv;
for(iv = 5.0; iv <= 10.0 ; iv += 0.01) {
/* stuff here */
}

Solution 4:[4]

int i;
double v;
v = 5;
for (i = 0; i < 500; i++)
{
    v += 0.01;
    // Do Calculations Here.

    if (v >= 10.00) break;
}

This gives you both. This will iterate at most 500 times, but will break out of that loop if the v value reaches (or exceeds) 10.00.

If you wanted only one or the other:

The 10.00 Version:

double v;
v = 5.0;
while ( v < 10.00 )
{
    v += 0.01;
    // Do Calculations Here. 
}

The 500 iterations version:

double v;
int i;
v = 5.0;
for( i = 0; i < 500; i++ )
{
    v += 0.01;
    // Do Calculations.
}

(Note that this isn't C99, which allows for a cleaner declaration syntax in the loops).

Solution 5:[5]

iv <= 10 doesn't do it for 10 loops, it does it until iv is greater than 10.

Solution 6:[6]

//start loop over v  
for(iv=0;iv<500;iv++) //loop from 0 to 499
{  
    v=v+0.01; //increase v by 0.01
    //Lots and lots of calculations with v here
}

this should do it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mateen Ulhaq
Solution 3 SiegeX
Solution 4 Reese Moore
Solution 5 Ignacio Vazquez-Abrams
Solution 6 bcosynot