Why for loop does not run by other than int variables? I tried to run for loop by taking float variable but it does not run by any other variables than int type?
Asked
Active
Viewed 142 times
-5
-
3Show some code or your question will be closed. – alex Jan 27 '13 at 05:54
-
Some code is always helpful. – Afonso Tsukamoto Jan 27 '13 at 05:56
-
btw, a working example: `int main(){ float i = 0.0; for(i=0.0; i<=3; i+=0.5){ printf("%.3f\n",i); } return 0; }` – Afonso Tsukamoto Jan 27 '13 at 06:00
-
This post answers your question and a lot more. Did you search the SO site before asking your question? [What is the full "for" loop syntax in C (and others in case they are compatible)?](http://stackoverflow.com/questions/276512/what-is-the-full-for-loop-syntax-in-c-and-others-in-case-they-are-compatible) – Jens Gustedt Jan 27 '13 at 08:12
2 Answers
1
You can use a for loop with integer variables, floating-point variables, even no variables at all.
int i;
for(i = 0; i < 10; i++) continue;
float f;
for(f = 0.0; f < 5; f += 0.5) continue;
for(;;) break;
But see What Every Computer Scientist Should Know About Float-Point Arithmetic for why you should think twice before using example 2.

luser droog
- 18,988
- 3
- 53
- 105
-1
You should be able to do it by using the STEP command
float X = 0;
//
//increase in steps of 1 x 1 thousandth
for (X = 1; X <= 100; X += 0.001) {
// DISPLAY YOUR RESULT maybe using: Math.Round(X, 3)
}

Zeddy
- 2,079
- 1
- 15
- 23
-
I just tried it on a web page in VB then converted it and left that line in – Zeddy Jan 27 '13 at 06:25
-
This question is tagged as C. Why are you using VB in your answer? – Philip Conrad Jan 27 '13 at 07:39