Breaking out of nested infinite while loops.

Discussion in 'C' started by Player, Nov 15, 2009.

  1. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone tell me how to do as the title says? I'm told that using goto is a no no. Here is some sample code that i can't seem to be able to figure out. I'm a novice by the way ;)
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	while(1)
    	{
    		printf("Hello.\n");
    		while(1)
    		{
    			while(1)
    			{
    				printf("How do i break out of this loop and back into the Hello loop?\n");
    			}
    		}
    	}
    return(0);
    }
     
    Last edited by a moderator: Nov 16, 2009
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    The "break" keyword will break you out of the loop:

    Code:
      while(1){
        printf("Stuck in the loop\n");
        if([I][COLOR="Blue"]somecondition[/COLOR][/I]){
          break;
        }
      }
      printf("Broke out of the loop\n");
    
    
     
  3. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    Hi. I know about break. How do you break from a multiple loop. Look at my example again. :)
     
  4. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    You'll have to use multiple breaks.
     
  5. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    My way :)
    Code:
    #include<stdio.h>
    #define TRUE 1
    #define FALSE 0
    
    int main(void)
    {
    	short int exitloop;
    	while(1)
    	{
    		exitloop=FALSE;
    		printf("Hello.\n");
    		while(1)
    		{
    			while(1)
    			{
    				printf("This is how i break this loop, back into the Hello loop\n");
    				exitloop=TRUE;
    				break;
    			}
    			if(exitloop==TRUE)
    				break;
    		}
    	}
    return(0);
    }
     
    Last edited by a moderator: Nov 17, 2009
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The correct answer depends very much on what you are trying to do. Nesting three infinite loops isn't a practical design for anything I've come across. Let us know what problem you're trying to solve and we'll see if we can come up with a better solution.

    On the other hand if this is just for academic interest, yeah, just use a goto, whatever works. Makes no difference.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice