generalized switch

Joshua Kosman kos at ernie.Berkeley.EDU
Mon Aug 4 09:46:32 AEST 1986


In article <2765 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <15093 at ucbvax.BERKELEY.EDU> kos at ernie.Berkeley.EDU (Joshua Kosman) writes:
>>	switch() {
>>		case <boolean-expr>:
>>		    etc.
>>		}
>>...
>>Any ideas?
>
>In C, such code is written:
>
>	if ( bool_expr_1 )
>		action_1
>	else if ( bool_expr_2 )
>		action_2
>	else if ...
>	else
>		default_action
>
>You could come up with some CPP macros for this, but why bother?

Sure, that's the way I have been doing it. But you can do that with
any choice among cases.
As I understand it, a switch/case setup compiles exactly the same as
	if (var == const1) {.....};
	else if (var == const2) {.....};
	else  {default_action};
anyway. (Or am i wrong?). In any case, it can be rewritten that way.
But the switch promotes comprehensibility. The situation I
find (mildly) frustrating is when I have a choice among cases, a
setup which is conceptually akin to a switch, but is not
syntactically equivalent because I want to use a slightly different test
than simple equality.


Joshua Kosman		||  "When I was young people used to tell me,
kos at ernie.berkeley.EDU	||	'When you're 50 you'll understand.' Well,
Dept. of Music		||	now I'm 50. I don't understand a thing."
UC Berkeley		||		-- Erik Satie



More information about the Comp.lang.c mailing list