Path: utzoo!attcan!utgpu!watmath!iuvax!rutgers!att!chinet!kdb From: kdb@chinet.chi.il.us (Karl Botts) Newsgroups: comp.lang.c Subject: Re: "arithmetic if":: Re: Feature for the next C version Message-ID: <9227@chinet.chi.il.us> Date: 10 Aug 89 05:12:45 GMT References: <55480@tut.cis.ohio-state.edu> <1989Jul20.152935.14872@utzoo.uucp> <67@motto.UUCP> <18764@mimsy.UUCP> <1389@crdgw1.crd.ge.com> <8515@batcomputer.tn.cornell.edu> <1429@crdgw1.crd.ge.com> <1287@atanasoff.cs.iastate.edu> Reply-To: kdb@chinet.chi.il.us (Karl Botts) Organization: Chinet - Public Access Unix Lines: 39 >>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.