goto jumps into loops
David Harmon
dmh at mit-borax.ARPA
Thu Apr 17 11:24:06 AEST 1986
Nice timing...Steve Summit's letter about jumping into blocks arrived
just as I had finished debugging a routine which I actually wrote in Pascal
and just translated into C for practice. You guessed it, it jumps into a loop,
and I honestly don't think there is a way to avoid the goto without some VERY
ugly code to keep track of the program's state. The problem is that I need
to print out all the elements of a set capable of containing or not containing
any value from 0..255. (In Pascal, a SET OF 0.255) That in itself would
be no problem given the IN operator, but I also wanted to contract adjacent
members of a set into a range syntax. Assume that the include file "nastydefs"
contains the definitions of the set type and in() function. Can any of you
figure out a way to do this elegantly without the goto? Especially this may
involve the break or continue statements...I have looked at possibilities but
I must admit I probably don't know a lot of tactics involving them.
Also, with an appropriate include file, this procedure both compiles and
passes lint.
Dave Harmon
dmh at mit-borax.arpa
dmh at borax.lcs.mit.edu
/* Assume the existence of a type named "set", which is intended to simulate
the Pascal TYPE KeySet:SET OF 0..255;
Further assume the existence of a function
in(e,l);
set *l;
char e;
which returns a boolean 1 iff _e_ is an element of _*l_.
*/
#include <stdio.h>
#include "/usr/dmh/nastydefs"
wrtlist (listp)
set *listp;
/*This routine should print out a list of the members of List, with adjacent
members shown contracted as N-M, where N and M are the bounds of a range of
members. Notice the GOTO statement which gives the effect of two *crossed*
loops with a Write and a conditional increment shared by both. Note that
the potential member 0 is not used by the application.
*/
#define MAX NUM_KEYWORDS /*The highest element in use by the application*/
{
unsigned char i;
set list;
i=0;
list= *listp;
do {
putchar(' ');
do {
if (i=MAX)
return(0);
else
i++;
}
while (in(i,&list));
cross: /*Beginning of GOTO Loop and shared section*/
printf("%d",i);
if (i=MAX)
return(0);
else
i++;
}
while (in(i,&list));
putchar('-');
while ( (i<MAX) && (in(i+1,&list) ) )
i++;
goto cross; /*End of GOTO Loop*/
} /*WriteKList*/
More information about the Comp.lang.c
mailing list