"arithmetic if":: Re: Feature for the next C version

Karl Botts kdb at chinet.chi.il.us
Thu Aug 10 15:12:45 AEST 1989


>>someone pointed out, I'm not looking for arithmetic IF, but a way to do
>>three separate things based on a value, usually the compare of two
>>values in a sort or tree search.
> 
>>  Some interesting ideas:
>>	ifcase
>>	  (a < b) code; code;
>>	  (a == b) more(code);
>>	  (a > b) still(more);
>>	endcase;

What you want is to extend the grammar from:

case-label
	constant-expression ':'

to

case-lobel
	expression ':'

Many languages allow the equivalent of this.  I don't think you'll see it
soon in C, though -- there are a couple of conflicts.  First, C puts
restrictions on the grammar in a number of places to permit better
optimization.   Good C compilers go to a lot of work to figure out how to
optimize case statements, often looking for consecutive or nearly
consective strings of case values so jump tables can be constructed,
figuring out how to test the variant for ranges to avaoid testing every
value individually, and so forth.  Arbitrary expressions would render most
of this optimimiztion impossible.

More importantly, C expressions can and do have wild and crazy side
effects, but if the compiler is going to do any optimization at all, then
the order in which the expressions (constant or otherwise) in the case labels
is evaluated, or even whether or not any particular one is evaluated at
all, must be undefined.  Furthermore, it _is_ undefined in the current
language definition.  To make the side effects of arbitrary expressions in
case-labels controllable, the order of their evaluation would have to be
defined.  This would be major, incompatible, change in the language.



More information about the Comp.lang.c mailing list