# Quick Question on For Loops

This is a discussion on Quick Question on For Loops within the C Programming forums, part of the General Programming Boards category; The following code is making no sense to me. When I put in Fahrenheit == UPPER it doesn't print out ...

1. ## Quick Question on For Loops

The following code is making no sense to me. When I put in Fahrenheit == UPPER it doesn't print out anything. However if I change it to <, it does. Also the first one or two examples of Celsius come out with some funky number.

I have no idea why either of these two things happen.

Can anyone tell me? Thanks in advance, and thanks to those who help out regularly in this forum, I have learnt a lot just by reading random posts.
Code:
```#include <stdio.h>
#include <stdlib.h>
#define LOWER 0
#define UPPER 300
#define STEP 20

int main () {

float Fahrenheit, Celsius;

for(Fahrenheit = 0; Fahrenheit == UPPER; Fahrenheit += STEP){
printf("%4.0f %6.1f\n",Fahrenheit,Celsius);
Celsius = (float) (5.0/9.0) * (Fahrenheit-32);

}
return(EXIT_SUCCESS);

}```

2. Thats because you are initalizing Fahrenheit to 0 and then immediatly doing a compare to see if it equals 300. Since 0 != 300 it exists the for loop without doing anything.

3. the loop will continue until the condition is false.
Fahrenheit is 0
while UPPER is 300
so Fahrenheit == UPPER is false thus the loop exits.
Try it with !=

4. Lol oh man, thanks for the quick responses guys.

I thought the loop continued until the condition was proved to be true..if it's until the condition is false then this makes total sense.

Thanks again.

5. I would NOT use !=

Fahrenheit is a float and 300 is an integer. There can be problems when comparing floats and ints (and even floats to floats) for strict equality. This is due to how floats store the numbers and basically some rounding that is involved.
300 is a number that can be expressed wholly the practice is still bad.

Use != when something should not be another value and use inequality when you are dealing with a range of numbers.

6. thought the loop continued until the condition was proved to be true..if it's until the condition is false then this makes total sense.
Until is the wrong word to use. While is what you are looking for.
C unlike pascal and some other languages has only one loop, while true. Granted there are multiple ways to express it (for, while, do while).

So the body of the loop will execute while the condition is true

7. Thanks man, that does make more sense.

I think what you are saying in your 2nd post might be happening in this program, because when I run it for my first value, the 0, I get some weird number I imagine is a number stored in my memory.

Is this the problem you were describing, or something altogether different? Or maybe it's a mathematical error since it's at 0.

8. Ah, I just rearranged the lines of code, and it works now.

I imagine it didn't work the first time, because it had no idea what the definition of Celsius was, but after running through the for loop once it had it..and could do the consequent calculations just fine.

Man, learning to program is exciting. Kind of reminds me when I first learned I could jackoff, I thought I was some kind of super hero.

9. The error I was describing shouldn't appear in this program. If you are having errors you might want to give us a sample of the output.

10. Was your error the problem that happens where the remainder basically gets eaten up between division of ints?

How would this happen between two floats?

11. Well thanks for the help bro, I'm going to get some sleep.

I'll check this out tomorrow.

12. Lets say you are using 32 bit integers. For even more ease we'll make them unsigned.

The problem can manifest itself with a loop such as:
Code:
```for(i=0; i < 4000000000; i++)
/* Compare i to some float */```
Any integer above 134217728 (2^27) is not accuretly represented down to the ones place. So if you change the loop to:
Code:
```int i;
float x = 134217739.0f;
for (i=0; i != x; i++)
/* Do something */```
There is the possibility that i will never equal exactly x and thus give you an infinate loop. But if you wanted to make sure that i didn't exceed x then changing the condition from != to < would work just fine.

13. Ah good stuff, thanks.