Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!teddy!jpn From: jpn@teddy.UUCP (John P. Nelson) Newsgroups: net.lang.c Subject: Re: goto jumps into loops Message-ID: <2517@teddy.UUCP> Date: Fri, 18-Apr-86 18:23:36 EST Article-I.D.: teddy.2517 Posted: Fri Apr 18 18:23:36 1986 Date-Received: Sun, 20-Apr-86 16:45:51 EST References: <69@brl-smoke.ARPA> Reply-To: jpn@teddy.UUCP (John P. Nelson) Organization: GenRad, Inc., Concord, Mass. Lines: 43 >Can any of you >figure out a way to do this elegantly without the goto? Wow! That code was UGLY! How about this: #include "nastydefs" #define MAX NUM_KEYWORDS /* 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. */ wrtlist(listp) register set *listp; { register int i; /* scan entire set */ for (i = 1; i < MAX; ++i) /* note: could just as easily start at 0 */ { if (in(i, listp)) { /* this is the start of a run (potential 1 element run) */ printf("%d", i); /* check for two in a row */ if (++i < MAX && in(i, listp)) { /* at least two in the run - scan to the end */ for (++i; i < MAX; ++i) if (!in(i, listp)) break; printf("-%d", i - 1); } } /* current i is known to NOT be in the set */ } } I assumed that the "in" function was expensive - any element is only checked ONCE. The secret is that the outermost level loop index is modified. A version with an auxiliary index is also possible, which is possibly slightly cleaner.