switch case for constants-only?

Ellery Newcomer ellery-newcomer at utulsa.edu
Sat Dec 5 19:36:47 PST 2009


On 12/05/2009 06:25 PM, Nick Sabalausky wrote:
>
> Also, I still don't see how there's any semantic difference between the
> current switch and the switch-like if-else chain other than just the fact
> that switch currently carries the restriction that the values being checked
> against must be unique and known at compile time.
>
>

If I understand switch correctly, it is analogous to this:

a = x;
if( a == exp1) goto Case1;
if( a == exp2) goto Case2;
...
if( a == expN) goto CaseN;
assert(0);
Case1:
  ...
Case2:
  ...
...
CaseN:
  ...

except that the case expressions aren't required to be in any order and 
the compiler has a fair amount of freedom in implementing those 
conditional jumps. Conceivably, you could even forgo the if statements 
and have computed goto statements if your cases are nice enough.

Also note the value being compared doesn't change while it is being 
compared. If statements are a bit less disciplined in that regard.



More information about the Digitalmars-d mailing list