comp.lang.c
[Top] [All Lists]

Re: case labels

Subject: Re: case labels
From: ozbear
Date: Sat, 29 Mar 2008 05:21:41 GMT
Newsgroups: comp.lang.c

On Sat, 29 Mar 2008 00:04:55 GMT, "Bartc" <bc@xxxxxxxxxx> wrote:

>christian.bau wrote:
>> On Mar 28, 5:48 pm, aark...@xxxxxxxxx wrote:
>>
>>> why are constant integer expressions required in case labels of the
>>> switch statement? what would be the impact of allowing general
>>> integer expressions instead of constant integer expressions? discuss
>>> both user convenience and implementation aspects?
>>
>> Consider
>>
>>   switch (i) {
>>     case f (): printf ("f"); break;
>>     case g (): printf ("g"); break;
>>   }
>>
>> Discuss under user convenience, implementation, and general mental
>> health aspects.
>
>No different to:
>
>if (i==f())
>    printf("f");
>else if (i==g())
>    printf("g");
>
>which is perfectly legal. It's a question of freedom of expression.
>

It is "no different to" the corresponding if-then-else ladder if
the semantics behind the proposed construct /said/ that that is 
so.  An equally valid meaning would be that all case labels are
evaluated and one is chosen based on the one that matched, and
/then/ one would have to describe what happens if two or more 
match.

Even if your meaming were chosen the compiler would have to 
be careful about any code reordering it might otherwise want to
do for the sake of optimisation, much as it already has to be
so as not to disturb the meaning of fall-thru cases.

Oz

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy