Thread: Stuck in simple while loop, can't break or return?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Stuck in simple while loop, can't break or return?

    Code:
    void printnode(Area*current){
    	while(1){
    	if(current->next==NULL){printf("[%p]", current->next); return;}
    	if(current->next!=NULL){printnode(current->next);}	
    	}
    }
    In this while loop, as I understand it, a structure Area*current is examined for the ->next. If that value is NULL, the value NULL should be printed and the break; return; should exit the function. Meanwhile, if *next is not null, then the function should recursively examine *next until it reaches a value of null. However, what I get is an infinite loop which just prints:

    Code:
    nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(nil)][(
    Why is this? If it is printing nil, then the value == null, and if the value == null, then it should return and exit the loop?
    Last edited by Adam Rinkleff; 07-28-2011 at 10:44 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nothing happens after a break; that's the point of a break. So there is no way to reach the return statement. If you want to return just return.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by tabstop View Post
    Nothing happens after a break; that's the point of a break. So there is no way to reach the return statement. If you want to return just return.
    break; was only there because return; wasn't working. I can take the break out but nothing changes.
    Last edited by Adam Rinkleff; 07-28-2011 at 10:49 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Given that current never advances inside your loop....

    Consider a three-item linked list. You call the function with 1; 1 calls 2; 2 calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns. 2 then loops and calls 3 which returns.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you trying to implement printnode iteratively or recursively? It looks like you are trying to do both at the same time, which is probably the problem.

    Also, is printnode supposed to print the current node in addition to all the following nodes? It looks like you only start printing from the next node.
    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

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Oh, I don't think I need the while(1) at all, its just not necessary.

    This is what I want:
    Code:
    void printnode(Area*current){
    	printf("\n<%d||[%p]>: <%p|%p>\n", current->name, current, current->prev, current->next);
    	if(current->next!=NULL){printnode(current->next);}
    	if(current->next==NULL){return;}
    
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would have written it as:
    Code:
    void printnode(Area *current) {
        if (current) {
            printf("\n<%d||[%p]>: <%p|%p>\n",
                   current->name, current, current->prev, current->next);
            printnode(current->next);
        }
    }
    Observe the tail recursion. It is easy to transform it to:
    Code:
    void printnode(Area *current) {
        while (current) {
            printf("\n<%d||[%p]>: <%p|%p>\n",
                   current->name, current, current->prev, current->next);
            current = current->next;
        }
    }
    In which case you would no longer have the limitations of recursion depth.
    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. Best way to break out of loop
    By Ducky in forum C++ Programming
    Replies: 31
    Last Post: 09-07-2010, 11:22 PM
  2. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  3. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  4. break or return
    By SAMSAM in forum Windows Programming
    Replies: 2
    Last Post: 01-30-2003, 09:48 PM
  5. Creating a Carriage-Return style Break in a program
    By JoeMomma5000 in forum C Programming
    Replies: 2
    Last Post: 10-29-2002, 01:06 AM