Thread: printf statement causing while loop to never end

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Post printf statement causing while loop to never end

    Hi, I'm working through Programming in C by Stephen G. Kochan and am at Chapter 8: Working with Functions. Exercise 8.4 has me add a printf statement to show the value of a float variable called 'guess' each time the while loop iterates.

    If I put the printf function below the calculation in the while loop all is good. When I experimented and added a second printf statement above the calculation I get the never-ending print.

    This was curious enough to post, as I wouldn't have thought a printf statement could impact anything to do with the actual while termination point.

    Thanking you in advance.

    Paul.

    here goes with the tags

    Code:
    float squareRoot (float x)
    {
        const float epsilon = 0.00001;
        float       guess   = 1.0;
    
    
        while (absoluteValue(guess * guess - x) >= epsilon)
            
            //take away the comment and the while loop goes forever.
            
            //printf("guess pre = %.2f\n", guess);
            guess = ( x / guess + guess ) / 2.0;
            printf("guess post = %.2f\n", guess);
    
    
        return guess;
    
    
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The problem has everything to do with you not bracketing the while loop. Indentation does not matter in the language, it is a convenience in order for humans to read it and that is all.

    If the code were to be indented the way that it would run (with the statement uncommented) it would look like this.

    Code:
    float squareRoot (float x)
    {
        const float epsilon = 0.00001;
        float guess = 1.0;
        
        while (absoluteValue(guess * guess - x) >= epsilon)
            printf("guess pre = %.2f\n", guess);
        
        guess = ( x / guess + guess ) / 2.0;
        printf("guess post = %.2f\n", guess);
        return guess;
    }
    So of course the loop runs forever as the guess never improves...
    Last edited by whiteflags; 05-31-2017 at 05:58 AM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    I strongly recommend using curly braces for all control statements, and loops, if, while, do-while, and for, even if there is only one statement to be executed in the block. It makes it clear for you and anyone else maintaining the code. Compare the following to your code and see how clear it reads:
    Code:
    float squareRoot(float x)
    {
       const float epsilon = 0.00001;
       float       guess   = 1.0;
     
       while(absoluteValue(guess * guess - x) >= epsilon)
       {
          // There is no question what statements are executed by this while statement! ;^)
          printf("guess pre = %.2f\n", guess);
          guess = ( x / guess + guess ) / 2.0;
       }  
          
       printf("guess post = %.2f\n", guess);
     
       return guess;
    }
    That is my #1 problem with python. It forces you to use indentation to define a block of code, instead of simple braces!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rstanley
    That is my #1 problem with python. It forces you to use indentation to define a block of code, instead of simple braces!
    Don't see a problem for Python because the language rules are different, hence indentation alone makes the flow of control clear. Because the rules are not so for C, indentation alone could be misleading, e.g., a mistake is possible under maintenance such that this:
    Code:
    if (x)
        foo();
    could become:
    Code:
    if (x)
        foo();
        bar();
    and the intent is not clear without context. Whereas in Python, the intent would be crystal clear without context. Hence, always using braces in C ensures that intent is clear even under maintenance, with indentation as a stylistic visual guide for the flow of control rather than both mechanism and visual guide.
    Last edited by laserlight; 05-31-2017 at 06:43 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-23-2015, 03:35 PM
  2. What's causing my program to use 99% CPU? inifinite loop?
    By superbbrr in forum C Programming
    Replies: 9
    Last Post: 11-08-2011, 03:41 PM
  3. Replies: 11
    Last Post: 06-10-2011, 01:17 PM
  4. ad-hoc link causing loop
    By Waldo2k2 in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-11-2005, 10:09 PM
  5. continue statement causing an unexpected error
    By drb2k2 in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2003, 06:46 AM

Tags for this Thread