Article View: comp.std.c
Article #3424Multi-level break and elimination of GOTO
From: Hey Sister can you spare a hug?
Date: Wed, 20 Sep 1995 16:02
Date: Wed, 20 Sep 1995 16:02
60 lines
2382 bytes
2382 bytes
Since I haven't seen this suggested yet, I'll throw it in and see what
happens...
It seems (to me) that one of the most needed places for a goto is inside a
multiply nested for or while loop of some sort. I have often wished for a
multi-level break statement in cases like this:
int x, y;
char map[MAXY][MAXX];
/* something filles in my 2D char array and I want to find the first
asterix inside it. */
for (x = 0; x < MAXX; x++)
{
for (y = 0; y < MAXY; y++)
{
if (map[y][x] == '*')
goto found_it;
}
}
found_it:
/* do more stuff */
It feels to me like what I _really_ want to do is this:
for (x = 0; x < MAXX; x++)
{
for (y = 0; y < MAXY; y++)
{
if (map[y][x] == '*')
break(2);
}
}
/* do more stuff */
where break(2); means break 2 levels out instead of the standard one level
afforded by break;. The standard break; would be equivalent to break(1);.
(At this point I suppose we could have a long discussion about having the
number be zero based or one based, but to me "break 2 levels out" in this
case feels more intuitive.)
One could make a similar case for the use of a multi-level continue
statement.
You would also need to think about the issue of run time vs. compile time
checking. If we demand that the argument to break be determinable at
compile time we run into fewer problems than we do if we allow any integer
expression and evaluate it at run time (although I suppose we could get
around this with the clever use of jump tables or some such).
--Adam
--
k g p m r e ,
o a @ a l o e o n t b i i a l s e
w z i l x . a l . d D o e i v n c r l n t e
a n a y c y u l e e y o h m
Technical Details
Message-ID:
<43pdut$hoh@galaxy.csc.calpoly.edu>Path:
arc1-ca.pugleaf.net!galaxy.csc.calpoly.edu!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!galaxy.csc.calpoly.edu!not-for-mail From: awozniak@galaxy.csc.calpoly.edu (Hey Sister can you spare a hug?) Newsgroups: comp.std.c,comp.lang.c Subject: Multi-level break and elimination of GOTO Date: 20 Sep 1995 09:02:37 -0700 Organization: Computer Science Department, Cal Poly SLO Message-ID: <43pdut$hoh@galaxy.csc.calpoly.edu> NNTP-Posting-Host: galaxy.csc.calpoly.edu NNTP-Posting-User: awozniak