want to avoid goto, how can I?

Started by
27 comments, last by kayX 18 years, 6 months ago
Quote:Original post by Conner McCloud
A slightly more eligent solution is simply miss1:;, but this is still a bit of a wart.
I'll admit that I missed this fact. I still consider the :; version easier to read, if not to write.
Writing this in the first place would most likely just result in a "compile -> error -> I'll just add a ; to shut up the compiler" sequence anyway (although it apparently didn't in this case).
Quote:Original post by Conner McCloud
As for ctr2==colno+1, I prefer ctr2>colno. ie, the opposite of the terminating condition. In addition to being more robust, it is more easily recognized as being 'the loop didn't find anything' rather than 'the loop found something at this specific value'.
True, but recognizing that this is what it means is nevertheless harder than following an explicit jump.
Reversing a <= into a > may be correct, but it's still far from intuitive for me. Had it been a simple < loop I think I would've agreed with you.

I'll admit that in this case the differences are small enough to be hard weight against each other however.
Advertisement
Quote:Original post by doynax
I'll admit that in this case the differences are small enough to be hard weight against each other however.

I was just about to say the exact same thing [grin]

CM
Quote:Original post by twanvl
I usually use a separate function in cases like this:
*** Source Snippet Removed ***


Seconded.

I can't really imagine, though, what the original code is actually doing that it has the illustrated structure. o_O
Most books say that using goto is "bad programming practice" which is probably why the OP wants to avoid it. In certain cases like this, I think there is nothing wrong with the use of goto. It is all contained in a small function and it isn't too hard to read. I think trying to avoid the goto makes it harder to read and understand. I agree that goto is bad, but when it is abused and makes the code ugly. This case isn't one of them, so........long live goto.


Thanks again for the help, the later replies make for good discussion/reading as well. The snippet is from this site over here for those interested. I'm actually a java programmer myself and am in the process of learning C. In doing so, I thought I'd port source from the language I'm learning to the language I already know. Honestly, outside of SPARC assembly I haven't seen a label/goto and that's the centre of my interest in todays exercise. I do appreciate the help and am actually feeling pretty dumb for spending 3 hours on that and not seeing how I could come up with a solution on my own.
---Real programmers don't comment their code. It was hard enough to write, it should be hard to understand!
Quote:Original post by WiLD2
doynax is correct. A goto is the simplest way to go about it.


I think this is pretty simple:

  ...  for(ctr1 = 0; ctr1 < globalN; ++ctr1)  {      for(ctr2 = 0; ctr2 <= colno; ++ctr2)      {	  if(theseHoldTrue)          {              myMethod(arr,colno+1,ctr1);              break;          }       }  }
theseHoldTrue is not defined.
Maybe I'm missing something, but can't the entire inner-loop be replaced by (unless this involves multithreading):
if (theseHoldTrue) {    ctr2 = colno;} else {    myMethod(arr, colno+1, ctr1);}
smr : I agree

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) <-- put a not
myMethod(arr,colno+1,ctr1);
break;
}
ctr1++;
}
}


This topic is closed to new replies.

Advertisement