Why do we have only costant value's or constant value expression in SWITCH statement
Hi,
Can anyone provide me some information about why do we have only constant value's or constant value expression be in SWITCH case statement ?
Is there any technical reason behind or its just compiler standard ?
Thanks
Kiran
Re: Why do we have only costant value's or constant value expression in SWITCH statem
Quote:
Originally Posted by rsodimbakam
Hi,
Can anyone provide me some information about why do we have only constant value's or constant value expression be in SWITCH case statement ?
Those are the rules of the language.
Regards,
Paul McKenzie
Re: Why do we have only costant value's or constant value expression in SWITCH statem
If the value can be change and no case will be match after changed.
Think logically.
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Because switch is a glorified goto and the targets are defined statically at compile time, not computed dynamically at run-time.
Or, as Paul put it, "them's the rules".
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by rsodimbakam
Can anyone provide me some information about why do we have only constant value's or constant value expression be in SWITCH case statement ?
Is there any technical reason behind or its just compiler standard ?
To my knowledge most (if not all) languages require the case labels to be compile-time constants. What differs is that in some languages the switch doesn't have to evaluate to an integer.
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by Graham
Because switch is a glorified goto
The switch offers a unique functionality namely that the case labels are evaluated without order. This cannot be mimiced by any combination of other control structures.
So the switch is not just a "glorified goto". It has a unique role among the common control structures.
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Please don't use apostrophes to make words plural. I can't understand why that simple little rule confuses so many people.
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by _uj
To my knowledge most (if not all) languages require the case labels to be compile-time constants. What differs is that in some languages the switch doesn't have to evaluate to an integer.
PHP is an exception, e.g.,
Code:
switch ($x)
{
case $a:
echo 'a';
break;
case $b:
echo 'b';
break;
case $a + $b:
echo 'a+b';
break;
default:
echo 'none of the above';
}
where $x, $a and $b are variables whose values are only known at run time.
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by GCDEF
Please don't use apostrophes to make words plural. I can't understand why that simple little rule confuses so many people.
Your picky :P
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by Hobson
Your picky :P
You're picky.
Yes, I'm a grammar cop, I'll admit. Still, most people here will be out in the professional world and proper use of the written language is important.
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by _uj
The switch offers a unique functionality namely that the case labels are evaluated without order. This cannot be mimiced by any combination of other control structures.
So the switch is not just a "glorified goto". It has a unique role among the common control structures.
What?
What does "case labels are evaluated without order" mean?
The only difference between switch and FORTRAN's computed GOTO is syntactic sugar. I could do anything that switch can do in FORTRAN. The only complication is that I would need to map (the used values of) the switch variable on to a consecutive sequence of integer values starting at one. That the compiler in C++ does this for me is, as I said, syntactic sugar.
E.g.
Code:
switch (i)
{
case 4:
// do stuff
break;
case 73:
// do other stuff
break;
default:
// more stuff
break;
}
in FORTRAN:
Code:
INTEGER J
IF (I.EQ.4) THEN
J = 1
ELSEIF (I.EQ.73) THEN
J = 2
ELSE
J = 3
ENDIF
GOTO(100, 200, 300) J
100 do stuff
GOTO 1000
200 do other stuff
GOTO 1000
300 more stuff
GOTO 1000
1000 ...
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by Graham
What?
What does "case labels are evaluated without order" mean?
The only difference between switch and FORTRAN's computed GOTO is syntactic sugar. I could do anything that switch can do in FORTRAN. The only complication is that I would need to map (the used values of) the switch variable on to a consecutive sequence of integer values starting at one. That the compiler in C++ does this for me is, as I said, syntactic sugar.
You can implement any control structure available in C++ in assembly and that would involve gotos. So if you would call all control structures "glorified gotos" I would agree, but if you single out the switch, I don't.
As a control structure the switch has a unique property. The label cases are in no specific order. That's what characterizes a switch and that's for example why an if-chain isn't functionally equivalent with a switch. That's also why a switch can be implemented in O(1) using say a table of some kind.
The fact that you (or a compiler) can accomplish the functional equivalent of a switch with gotos in some language is irrelevant. The switch has a unique property that no combination of other control structures can mimic. It's not just a "glorified goto".
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by laserlight
PHP is an exception, e.g.,
Code:
switch ($x)
{
case $a:
echo 'a';
break;
case $b:
echo 'b';
break;
case $a + $b:
echo 'a+b';
break;
default:
echo 'none of the above';
}
where $x, $a and $b are variables whose values are only known at run time.
Isn't PHP an interpreted language (like PERL)?
Viggy
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by MrViggy
Isn't PHP an interpreted language (like PERL)?
Yes, PHP code is typically interpreted at run time (after all, C++ is also interpreted, but at compile time, heh).
Re: Why do we have only costant value's or constant value expression in SWITCH statement
Quote:
Originally Posted by laserlight
Yes, PHP code is typically interpreted at run time (after all, C++ is also interpreted, but at compile time, heh).
Heh, well, unless you're going to directly write in machine code every language has to be interpreted at some point. ;)