[comp.lang.c] switch break

poser@csli.Stanford.EDU (Bill Poser) (10/03/90)

I'm curious about the problem some people apparently have
with the fall-through property of switch. I've never had the
slightest difficulty with this, even when I was first learning
C. Modestly putting aside the attractive hypothesis that I am
a genius at learning programming languages, I wonder if
the people who have a problem are those who have previously
learned a similar construction in a language that lacks fall-through,
such as the Pascal case statement? When I learned C, I knew Fortran
(Fortran IV, the real thing, not something semi-modern like F77),
a bit of BASIC, MIX assembler (only on paper), and the assembler
for an obscure Japanese laboratory minicomputer. None of these
had anything like switch, so I had no preconceptions about how
it should work. But an awful lot of people these days come to
C after learning Pascal. Are they the people who are having
trouble with this?

						Bill

ark@alice.att.com (Andrew Koenig) (10/03/90)

In article <15611@csli.Stanford.EDU>, poser@csli.Stanford.EDU (Bill Poser) writes:

> I'm curious about the problem some people apparently have
> with the fall-through property of switch.

Some people have difficulty understanding things that aren't Politically Correct.
-- 
				--Andrew Koenig
				  ark@europa.att.com

stelmack@screamer.csee.usf.edu (Gregory M. Stelmack) (10/03/90)

OK, I can't resist anymore :-)

I thought one of the nice things about C was the programmer's ability to do
anything, regardless of whether or not it makes sense. If you use the break;
you can separate each case. If you don't, you allow follow-through. It seems
to me that if this flexibility makes it hard for you to work with, maybe C
isn't the language for you. I personally like having the compiler allow me to
do whatever I want without delving into Assembler -- that's why I use C
instead of other languages.

Am I off-base with this opinion?

-- Greg Stelmack
-- Email: stelmack@sol.csee.usf.edu
-- USmail: USF Box 1510, Tampa, FL 33620-1510
-- Amiga: the only way to compute!

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (10/03/90)

In article <11428@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
>In article <15611@csli.Stanford.EDU>, poser@csli.Stanford.EDU (Bill Poser) writes:
>
>> I'm curious about the problem some people apparently have
>> with the fall-through property of switch.
>
>Some people have difficulty understanding things that aren't Politically 
>Correct.


And some of us have problems with unjustified snipes.

I doubt anyone has difficulty understanding the semantics of switch,
they are simple enough. That doesn't mean we agree with them. Any decent
C programmer uses switch correctly every day of eir life, but I doubt
that I am alone in having omitted it by accident once or twice and
having been left with annoying bugs.

We don't want to take away your precious fall-through cases, I'm glad that
option is available too. But the each case separate type of switch
clearly is the more common type, is much easier to follow, and is
more correct politically, err, I mean is more structured. 

Besides, it always seems silly to me to have to write break a dozen
times in a long switch to do what in my mind is the canonical 
operation. Changing the default to breaking and allowing the word
continue to be used for fall-through  where would make switch statements 
cleaner and would save typing in both the fall through and other case --- 
we wouldn't have to type "break;" and we wouldn't have to type
"/* YES --- I REALLY MEAN TO FALL THROUGH */."



--
Dave Eisen                      	    Home: (415) 323-9757
dkeisen@Gang-of-Four.Stanford.EDU           Office: (415) 967-5644
1447 N. Shoreline Blvd.
Mountain View, CA 94043

ark@alice.att.com (Andrew Koenig) (10/04/90)

In article <1990Oct3.160403.517@Neon.Stanford.EDU>, dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:
> In article <11428@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
> >In article <15611@csli.Stanford.EDU>, poser@csli.Stanford.EDU (Bill Poser) writes:

> >> I'm curious about the problem some people apparently have
> >> with the fall-through property of switch.

> >Some people have difficulty understanding things that aren't Politically 
> >Correct.

> And some of us have problems with unjustified snipes.

I quite agree.  However, I went back and read the original posting
and my response and cannot find any unjustified snipes there.
To what are you referring?
-- 
				--Andrew Koenig
				  ark@europa.att.com

enag@ifi.uio.no (Erik Naggum) (10/04/90)

In article <1990Oct3.160403.517@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:

   Besides, it always seems silly to me to have to write break a dozen
   times in a long switch to do what in my mind is the canonical 
   operation.

May I suggest

	#define Case break; case

--
[Erik Naggum]		Naggum Software; Gaustadalleen 21; 0371 OSLO; NORWAY
	I disclaim,	<erik@naggum.uu.no>, <enag@ifi.uio.no>
  therefore I post.	+47-295-8622, +47-256-7822, (fax) +47-260-4427

browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) (10/05/90)

About the fact that a case in a switch statement falls through to the next
case unless there's an explicit break:

In article <1990Oct3.160403.517@Neon.Stanford.EDU>, 
dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:
> I doubt anyone has difficulty understanding the semantics of switch,
> they are simple enough. That doesn't mean we agree with them. Any decent
> C programmer uses switch correctly every day of eir life, but I doubt
> that I am alone in having omitted it by accident once or twice and
> having been left with annoying bugs.

Bingo.

The above is my own opinion and not attributable to any other person or
organization.                        email: browns@iccgcc.decnet.ab.com
Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.    (216) 371-0043

roy%cybrspc@cs.umn.edu (Roy M. Silvernail) (10/05/90)

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:

> Changing the default to breaking and allowing the word
> continue to be used for fall-through  where would make switch statements 
> cleaner and would save typing in both the fall through and other case --- 

Continue, though, is already a keyword for loop control. Giving it two
seperate and context-dependant tasks would, IMHO, _really_ screw people
up. I'll keep the breaks, thank you, and the inherent lack of
constriction C allows.
--
Roy M. Silvernail |+|  roy%cybrspc@cs.umn.edu  |+| #define opinions ALL_MINE;
main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);}
"This is cyberspace." -- Peter da Silva  :--:  "...and I like it here!" -- me

kpicott@alias.UUCP (Socrates) (10/05/90)

In <1990Oct3.160403.517@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:

>Changing the default to breaking and allowing the word
>continue to be used for fall-through  where would make switch statements 
>cleaner and would save typing in both the fall through and other case --- 
>we wouldn't have to type "break;" and we wouldn't have to type
>"/* YES --- I REALLY MEAN TO FALL THROUGH */."
>Dave Eisen                      	    Home: (415) 323-9757

As long as we're changing the definition of C why not just add another
keyword "fallthrough" to be used instead of "continue"?  That way we have
our structuring, we have our ability to fallthrough, and the loop functions
"break" and "continue" can be consistent in their usage.

--
 Kevin Picott   aka   Socrates   aka   kpicott%alias@csri.toronto.edu
 Alias Research Inc.  110 Richmond St. E.  Toronto, Ontario... M5C 1P1
 (416) 362-9181 x336

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (10/05/90)

In article <RVJJq3w163w@cybrspc> roy%cybrspc@cs.umn.edu (Roy M. Silvernail) writes:
>dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:
>
>> Changing the default to breaking and allowing the word
>> continue to be used for fall-through  where would make switch statements 
>> cleaner and would save typing in both the fall through and other case --- 
>
>Continue, though, is already a keyword for loop control. Giving it two
>seperate and context-dependant tasks would, IMHO, _really_ screw people
>up. 

Break, though, is already a keyword for loop control. Giving it two
separate and context-dependent tasks would, IMHO, _really_ screw people
up.

Oh hell, I guess they'll muddle through somehow.




--
Dave Eisen                      	    Home: (415) 323-9757
dkeisen@Gang-of-Four.Stanford.EDU           Office: (415) 967-5644
1447 N. Shoreline Blvd.
Mountain View, CA 94043

jh4o+@andrew.cmu.edu (Jeffrey T. Hutzelman) (10/06/90)

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:

> In article <RVJJq3w163w@cybrspc> roy%cybrspc@cs.umn.edu (Roy
> M. Silvernail) writes:
>> Continue, though, is already a keyword for loop control. Giving it two
>> seperate and context-dependant tasks would, IMHO, _really_ screw
>> people up. 
>
> Break, though, is already a keyword for loop control. Giving it two
> separate and context-dependent tasks would, IMHO, _really_ screw
> people up.

Perhaps, but break already _has_ been given that meaning and plenty of C
programmers deal with it all the time with no problem, and, IMHO, it
makes sense.
-----------------
Jeffrey Hutzelman
America Online: JeffreyH11
Internet/BITNET:jh4o+@andrew.cmu.edu, jhutz@drycas.club.cc.cmu.edu

>> Apple // Forever!!! <<

kdq@demott.COM (Kevin D. Quitt) (10/06/90)

In article <ENAG.90Oct4022413@hild.ifi.uio.no> enag@ifi.uio.no (Erik Naggum) writes:
>In article <1990Oct3.160403.517@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:
>
>   Besides, it always seems silly to me to have to write break a dozen
>   times in a long switch to do what in my mind is the canonical 
>   operation.
>
>May I suggest
>
>	#define Case break; case

    Perhaps even better (because it won't annoy the maintenance
programmer) is to define yourself an editor macro called something like
"make-switch-case" which inserts the "case :<NL> and "break;" (properly
tabified, of course), and leaves your cursor in front of the colon.  This
does assume you are using a reasonable editor. 

-- 
 _
Kevin D. Quitt         demott!kdq   kdq@demott.com
DeMott Electronics Co. 14707 Keswick St.   Van Nuys, CA 91405-1266
VOICE (818) 988-4975   FAX (818) 997-1190  MODEM (818) 997-4496 PEP last

                96.37% of all statistics are made up.

roy%cybrspc@cs.umn.edu (Roy M. Silvernail) (10/07/90)

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:

> In article <RVJJq3w163w@cybrspc> roy%cybrspc@cs.umn.edu (Roy M. Silvernail)
> writes:
> >Continue, though, is already a keyword for loop control. Giving it two
> >seperate and context-dependant tasks would, IMHO, _really_ screw people
> >up. 
> 
> Break, though, is already a keyword for loop control. Giving it two
> separate and context-dependent tasks would, IMHO, _really_ screw people
> up.
> 
> Oh hell, I guess they'll muddle through somehow.

Actually, Dave, I didn't really present my point clearly. I should have
said "seperate, context-dependant and _differing_ tasks".

The break keyword, of course, is used now to:

1) terminate a case in the switch statement.

2) force immediate termination of a for, while or do loop.

Thinking about this, I find both uses consistant with each other, as
they both stop the current iteration of a control structure.

The suggested addition to continue, though, isn't consistant. Currently,
continue causes the current control structure to reiterate. The
suggested use within a switch would do exactly the opposite, and cause
the control structure to progress. A switch can be viewed as a loop that
only executes once. Redefining continue within a switch? Just a disaster
waiting to compile. (ask the nameless AT&T programmer about break vs.
if...)

But you're right... we'll all muddle through. ;-)
--
Roy M. Silvernail |+|  roy%cybrspc@cs.umn.edu  |+| #define opinions ALL_MINE;
main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);}
"This is cyberspace." -- Peter da Silva  :--:  "...and I like it here!" -- me