Thread: How to get to an outer while

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    How to get to an outer while

    Hi everyone. I've written a search and replace routine in C99. I think it should work, but there is a point marked by the // where I want to restart the outer while loop. How do I do this?

    Richard (TIA)

    Code:
    char *ptokens[][2] = 
    {
       { "Find this" , "Replace with this" } ,
       { "Another one" , "Here's the replacement." } ,
       { 0 , 0 }
    } ;
    
    int searchandreplace ( char* psource , char* pdestination ) 
    {
    	while ( *psource != 0 )
    	{
    		int i = 0 ;
    
    		while ( ptokens [i][0] != 0 )
    		{
    			char* ptoken = strstr ( psource , ptokens [i][0] ) ;
    			if ( ptoken == psource )
    			{
    				strcpy ( pdestination , ptokens [i][1] ) ;
    				psource += strlen ( ptokens [i][0] ) ;
    				pdestination += strlen ( ptokens [i][1] ) ;
    				// start outer while again
    			}
    			else
    				i++ ;
    		}
    
    		*pdestination ++ = *psource ++ ;
    	}
    
    	*pdestination = 0 ;
    
    	return 0 ;
    }
    Last edited by Richardcavell; 03-05-2011 at 01:37 AM. Reason: fix stupid bug

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is actually no direct way to jump back out through two levels of nested loops.

    There are various indirect ways.

    The simplest way, although it would make "anti-goto" zealots incredibly unhappy, is to place a label at the start of the outer loop, and then "goto that_label;".

    Another way might involve using negative values of i as an indicator that the code needs to start again;
    Code:
    while ( *psource != '0' )
    	{
    		int i = 0;
    
    		while ( i >= 0 && ptokens [i][0] != 0)
    		{
    			char* ptoken = strstr ( psource , ptokens [i][0] ) ;
    			if ( ptoken == psource )
    			{
    				strcpy ( pdestination , ptokens [i][1] ) ;
    				psource += strlen ( ptokens [i][0] ) ;
    				pdestination += strlen ( ptokens [i][1] ) ;
    				i = -1;   /*  signal breakout */
    			}
    			else
    				i++ ;
    		}
    
    		if (i >= 0) *pdestination ++ = *psource ++ ;
    	}
    More generally, however, it would be better to restructure your code to reduce (or, better, eliminate if you can) the need to back out of multiple nested loops. Code that arbitrarily backs out of multiple nested loops tends to be somewhat difficult to understand, and therefore harder to get right - and rather fragile if you need to change the logic later on.
    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. Can a inner class have a member of outer class type?
    By pheres in forum C++ Programming
    Replies: 9
    Last Post: 10-06-2009, 07:04 AM
  2. SQL like outer join using LINQ
    By indigo0086 in forum C# Programming
    Replies: 2
    Last Post: 08-12-2008, 10:27 AM
  3. find the outer polygon enclosing a set of points
    By itachi in forum C Programming
    Replies: 1
    Last Post: 02-18-2006, 03:38 AM
  4. problem with fread and fwrite? bug?
    By moonlord in forum C Programming
    Replies: 17
    Last Post: 08-21-2005, 09:31 AM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM

Tags for this Thread