[comp.lang.c] the default of a switch

klatchko@cory.Berkeley.EDU (ron klatchko) (03/09/89)

In a C switch statement, if you have a 'default' part, does it have 
to go below all the 'case' parts?  As a matter of style, I know it
should, but how about offical C syntax?  How about the ANSI standard?
Thanks.

ron

P.S.  please send replys to this account

--------------------------------------------------------------------------------
Ron Klatchko          klatchko@cory.Berkeley.EDU        ...!ucbvax!cory!klatchko   

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/10/89)

In article <10833@pasteur.Berkeley.EDU> klatchko@cory.Berkeley.EDU (ron klatchko) writes:
>In a C switch statement, if you have a 'default' part, does it have 
>to go below all the 'case' parts?

It's just one of several possible labels in the switch body; the labels
can appear on any statement.

>As a matter of style, I know it should, ...

Not necessarily.  There are times when the default case is better dealt
with first.

bph@buengc.BU.EDU (Blair P. Houghton) (03/10/89)

In article <10833@pasteur.Berkeley.EDU> klatchko@cory.Berkeley.EDU (ron klatchko) writes:
>In a C switch statement, if you have a 'default' part, does it have 
>to go below all the 'case' parts?  As a matter of style, I know it
>should, but how about offical C syntax?  How about the ANSI standard?
>Thanks.

The ref pages in K&R I are not specific.  I assume that means You Can
Put it Anywhere within the scope of the switch.  You can do the same
with the 'case:' statements, too, although the status of
'case const-expr: statememt' as a statement itself means that you can
make 'case const-expr: statement' part of the 'statement' in
a preceding 'case const-expr: statement', effectively creating the
fall-through execution we're all familiar with.

Do K&R II or the pANS say anything more restrictive
about 'default: statement' ?

(Ten seconds and one index-dereferencing later...)
Sorry. "RTFM, Blair!"  Page 55, K&R I, second-to-last sentence, says,
"Cases and default can occur in any order."

				--Blair
				  "Ready everyone? In your
				   best Emily Litella voice:
				   'never mind.'"