"Break" and other ways out of loops and switches
Curt Noll
chongo at nsc.uucp
Thu Aug 25 06:04:10 AEST 1983
> ... A high school student (!) that I help with his compiler over
> occassional lunch hours came up with what I think is a super way out of
> the multiple-level or multiple-construct break problem (i.e., breaking
> out to the "right" level (the one you want)). He suggested labelling the
> initial statement of the construct to be "broken" ... and then giving
> the label as an "argument" to break.
> ... other languages (such as BLISS) have this feature. I think that
> it's great, would be relatively easy to add to compiler (I think),
> and is even compatible with the current C. Multiple-level breaks are
> a common reason for seeing one of those rare goto's in modern code.
> This feature would eliminate the need those occurrances. Well,
> what do do you think, yea or nay?
>
> Chris Scussel Bell Labs
i say NAY to the high school suggestion regarding labeled multi-break.
what that would do is to create another quasi-goto, and another
way for obscene code to be created.
try:
/* more or less minus possible typos */
#define TRUE 1
#define FALSE 0
#define breakout( breakflag ) if ( breakflag ) { break; }
#define multibreak( breakflag ) breakflag = TRUE; break
#define setbreak( breakflag ) breakflag = FALSE
#define testbreak( breakflag ) ! breakflag
#typedef unsigned char BREAKFLAG
...
BREAKFLAG stopreading; /* read process termination */
...
/* read lines of text ... */
for ( setbreak( stopreading ); some-code;
some-code && testbreak( stopreading ) ) {
...
/* character in line processing */
while ( some-code && testbreak( stopreading ) ) {
...
/* case in char in a line */
switch ( some-code ) {
...
case SOMETHING:
...
multibreak( stopreading ); /* stop reading */
break; /* for readability */
...
}
breakout( stopreading ); /* termiate loop */
}
breakout( stopreading ); /* terminate loop */
}
any number of ways can be constructed in C to do similar things.
chongo /\../\
More information about the Comp.lang.c
mailing list