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. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    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. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    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. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    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. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    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. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    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. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    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. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Well thanks for the help bro, I'm going to get some sleep.

    I'll check this out tomorrow.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Ah good stuff, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-27-2005, 12:05 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21