want to avoid goto, how can I?

Started by
27 comments, last by kayX 18 years, 7 months ago
I just can't see how to do this. I should be able to complete this function without using a goto, shouldn't I? I can't see how it can be done, though. And the last three hours of my life have been dedicated to staring dumbly at this snippet for the sake of proving I can do it. I've given up and am resorting to help from others. Am I really missing the obvious, and how can I do this? Thanks.
void myMethod(int arr[], int colno, int val)
{
  int ctr1,ctr2;
  a1[colno]=val;
  
if(colno==N-1)
    {
      printArray(a1); return;
    };

  for(ctr1 = 0; ctr1 < globalN; )
    {
      for(ctr2 = 0; ctr2 <= colno; ctr2++)
	{
	  if(theseHoldTrue)
	    goto miss1;
	}
      myMethod(arr,colno+1,ctr1);
    miss1:
      ctr1++;
    }
}
[edit] sorry for the confusion as I edited the source to try to avoid clutter and confusion.
---Real programmers don't comment their code. It was hard enough to write, it should be hard to understand!
Advertisement
I see no obvious solutions other than splitting the innerloop out into a separate function and letting it return a boolean to decide whether someOtherMethod should be called, or possibly using additional state variables to determine how the loop was exited.
You'll have to decide for yourself it that's any easier to read. But personally I have no problems with the goto approach in this case (at least in that short example).
Might not be the best way but it works I think.

void myMethod(int arr[], int colno, int val){  int ctr1,ctr2;  bool b = false;  for(ctr1 = 0; ctr1 < globalN; )    {      for(ctr2 = 0; ctr2 <= colno; ctr2++)	{	  if(a1[ctr2]==ctr1 || (colno+1-ctr2)*(colno+1-ctr2)==(ctr1-a1[ctr2])*(ctr1-a1[ctr2]))          {            b = true;	    break;          }	}      if(!b)      {        someOtherMethod(a1,colno+1,ctr1);      }      ctr1++;      b = false;    }}
I think this will work
 for(ctr1 = 0; ctr1 < globalN; )    {      for(ctr2 = 0; ctr2 <= colno; ctr2++)	{	  if(theseHoldTrue)	    break;	}      if ( ctr2 == colno+1 )          myMethod(arr,colno+1,ctr1);          ctr1++;    }
weasalmongler:
krum:
Both of those should work but the real question is if they are any easier to interpret than a goto? Again my personal opinion is that they're significantly worse. If you don't want to do that then splitting things off into a separate function is the standard approach.

Considering the complexitly of that inner if-condition I'd say that this part is relatively insignificant however.
doynax is correct. A goto is the simplest way to go about it. However, another programming language, such as java, doesn't have an easy way to do a goto statement.

Those were quicker replies than I'd anticipated and my editing seems to have resulted in 2 or 3 different source quotes (whoops). And I can't believe how I had missed the use of a boolean. Thxa for the help.
---Real programmers don't comment their code. It was hard enough to write, it should be hard to understand!
Ok, it's late and I've only spent a minute looking at the code, so I might just have missed something, but wouldn't this do the trick as well?

void myMethod(int arr[], int colno, int val){  int ctr1,ctr2;  a1[colno]=val;  if(colno==N-1)    {      printArray(a1); return;    };  for(ctr1 = 0; ctr1 < globalN; ctr1++ )    {      for(ctr2 = 0; ctr2 <= colno; ctr2++)	{	  if(theseHoldTrue){	    continue;          }	}      myMethod(arr,colno+1,ctr1);    }}

Quote:Original post by WiLD2
doynax is correct. A goto is the simplest way to go about it. However, another programming language, such as java, doesn't have an easy way to do a goto statement.
Java has multi-level break however, which seems like the equivalent solution in this case.
Quote:Original post by Spoonbender
Ok, it's late and I've only spent a minute looking at the code, so I might just have missed something, but wouldn't this do the trick as well?
Unfortunately the continue statement will apply to the innerloop and not the outer one.
just fyi, it has been proven that every algorithm conceivable can be constructed without the use of goto.
Is this C or C++? If it's C++, you could do something like this:
struct Miss { };// ...      try {        for(ctr2 = 0; ctr2 <= colno; ctr2++)          {	    if(theseHoldTrue)	      throw Miss();          }        myMethod(arr,colno+1,ctr1);        } catch (Miss) {          ctr1++;        }


But really, that's really not that different than a goto (except it's more obvious where the exception is transferring control to).

This topic is closed to new replies.

Advertisement