Thread: How does this while loop work?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    How does this while loop work?

    The below function confuses me. Basically, since unsigned integers use a two's complement system, we cannot take the inverse of n before processing it, should n be negative. Because the largest negative number does not have absolute value representation. Thus, we use abs here after processing n % 10 while n is still negative. But how does that while loop work? Why doesn't it loop forever? If n is -133, reducing it by a factor of 10 with each iteration, it will start spitting out -1 infinitely. Am I missing something?

    Code:
    void itoa(int n, char s[]) {
    
        int i, sign;
    
        sign = n;
    
        
    
        i = 0;
    
        do {
    
            s[i++] = abs(n % 10) + '0';
    
        } while ( n /= 10 );
    
        if (sign < 0)
    
            s[i++] = '-';
    
        s[i] = '\0';
    
        reverse(s);
    
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    They're using integer math, anything less then 1 is 0 , thus the loop ends... -1/10 = 0.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    62
    But if n is a negative value, n will always be less than 1 and therefore the loop will never execute, if what you say is correct, no?

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The loop ends when n /= 10 results in 0. Not less then zero, not greater then zero, exactly zero.

    Besides that it's a do while loop and will always execute at least once.

    EDIT: Why don't you put a printf("n = %d\n",n) right above (and/or below) line 13 and see what n is on each iteration of the loop.
    Perhaps that will clear things up for you.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonpuz View Post
    They're using integer math, anything less then 1 is
    That's not true. Integer match rounds toward zero. So -21/10 is -2, not -3.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why won't my loop work?
    By QuinnSF in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2014, 11:58 PM
  2. Why will my loop does not work
    By atoivan in forum C Programming
    Replies: 2
    Last Post: 11-10-2011, 11:21 AM
  3. Can't get cout to work in for() loop
    By motarded in forum C++ Programming
    Replies: 1
    Last Post: 03-03-2006, 09:00 AM
  4. Can't get the loop to work
    By pass_prime in forum C Programming
    Replies: 10
    Last Post: 06-20-2005, 03:46 PM
  5. cant get the loop to work right
    By djxtremor in forum C Programming
    Replies: 3
    Last Post: 10-30-2002, 06:34 AM