'goto" gone from computer languages or is it!

Sean 'Captain Napalm' Conner spc at conman.org
Mon May 16 14:26:46 CDT 2005


It was thus said that the Great Jim Battle once stated:
> 
> John Hogerhuis wrote:
> 
> > The other one I use it for is jumping past the top of a loop the first
> > time only (this code probably doesn't work but you get the idea...):
> > 
> > void print_ary (int *aryp, int n)
> > {
> > 
> >    goto skip_comma;
> >    for (;n;aryp++, n--)
> >       {
> >          printf (", ");
> > 
> > skip_comma:
> >           printf ("%u", *aryp);
> >       }
> >    printf ("\n");
> > 
> > }
> 
> I'd do this, and didn't even know that jumping into the middle of the 
> FOR loop was legal.  Even knowing it is legal, I prefer my way (of course):
> 
> void print_ary (int *aryp, int n)
> {
>     int i;
>     for (i=0; i<n; aryp++, i++)
>        {
>            if (i>0)
>                printf (", ");
>            printf ("%u", *aryp);
>        }
>     printf ("\n");
> }

  I'd skip both the GOTO and the conditional and do it:

	void print_ary(int *aryp,size_t n)
	{
	  size_t  i;
	  char   *sep;

	  assert(aryp != NULL);		/* sorry, gotta check */
	  assert(n    > 0);

	  for (i = 0 , sep = "" ; i < n ; aryp++ , i++)
	  {
	    printf("%s%u",sep,&aryp);
	    sep = ",";
	  }
	  putchar('\n');
	}

  -spc (Who tries to avoid conditionals when possible ... )



More information about the cctalk mailing list