|
-
September 25th, 2008, 12:24 AM
#1
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
-
September 25th, 2008, 02:47 AM
#2
Re: Why do we have only costant value's or constant value expression in SWITCH statem
 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
-
September 25th, 2008, 03:02 AM
#3
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.
-
September 25th, 2008, 04:01 AM
#4
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".
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 25th, 2008, 05:56 AM
#5
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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.
Last edited by _uj; September 25th, 2008 at 06:30 AM.
-
September 25th, 2008, 06:21 AM
#6
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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.
-
September 25th, 2008, 07:28 AM
#7
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.
-
September 25th, 2008, 07:43 AM
#8
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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.
-
September 25th, 2008, 08:23 AM
#9
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
September 25th, 2008, 08:53 AM
#10
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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.
-
September 25th, 2008, 09:02 AM
#11
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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 ...
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 25th, 2008, 12:21 PM
#12
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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".
Last edited by _uj; September 25th, 2008 at 04:14 PM.
-
September 25th, 2008, 02:58 PM
#13
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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
-
September 25th, 2008, 03:05 PM
#14
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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).
-
September 25th, 2008, 07:32 PM
#15
Re: Why do we have only costant value's or constant value expression in SWITCH statement
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|