switch (i)
{
case 1 : <simple-statement>;
case 2 : <simple-statement>;
case 3: {
<any number of statements>
goto case 1;
}
default: <simple-statement>
}
compiles cleanly and gives all the flexibility required, avoids jumping into
blocks.
In C++ the error-prone, but defined, behaviour of dropping through to the
next case was a reason to have break statements.
In C## this is not allowed. You must always write your code with a break
statement (except in the goto case).
Again, apart from the argument that C++ uses break statements, is there ever
any point to having them in C#?
Phil Wigglesworth
Mythlandia, Inc.
(assumes i is an integer)
switch (i)
{
case 1 :
case 2:
case 3: <any number of statements>
Break
default: <simple-statement>
}
Personally, I don't see why they didn't just ditch Switch in favor of Select
Case.
--
Jonathan Allen
"Phl Wigglesworth" <phil.wig...@mythlandia.com> wrote in message
news:#oKt2zu3AHA.2168@tkmsftngp07...
Phil Wigglesworth
Mythlandia, Inc
"Jonathan Allen" <grey...@cts.com> wrote in message
news:OeFzcDw3AHA.2172@tkmsftngp07...
Complete compared to what? Since is lacks ranges and comparisons, it isn't
as flexible as VB.
Consider a simple grading scale...
Select Case X ' which is a decimal
Case 0 To 59
'F
Case 59 To 69
'D
Case 69 To 79
'C
Case 79 To 89
'B
Case 89 To 100
'A
Case Else
'Error
End Select
I wouldn't want to even attempt this with C#. First you would have to
convert X to a Integer. Then you would have 100 case lines.
--
Jonathan Allen
"Phl Wigglesworth" <phil.wig...@mythlandia.com> wrote in message
news:ehppRMx3AHA.228@tkmsftngp03...
If you check the documentation, you will learn that there is an extra
statement "goto case #", that allows yoyu to switch between the case statements.
Sometimes it's extremely usefull to use it when you need to do something in a
sequence, i.e. letting the code "fall through" the cases.
Thomas Petersen
MCSD Developer
behind the scenes. The traditional role of the switch/case statement has
been to focus the implementation as a lookup table - hence the insistance on
integers and compile-time verification of labels.
Phil Wigglesworth
Mythlandia, Inc
"Jonathan Allen" <grey...@cts.com> wrote in message
news:#HBWlYx3AHA.2064@tkmsftngp05...
What rule says that lookup tables have to be integer based? I use select
case on Strings more than anything else.
--
Jonathan Allen
"Phl Wigglesworth" <phil.wig...@mythlandia.com> wrote in message
news:OGWLSBy3AHA.2236@tkmsftngp07...
The last statement in a case block is not the only place break statements
are used.
Case 1:
<any number of statements>
if (<some condition>)
break;
<any number of statements>
Case 2
<etc>
That is a crude example and can easily be implemented with an if-else, but
there are more complicated situations as well.
-W
Breaking out of loops.
--
Marko
ICQ: 5990814
Hobbes: How come we play war and not peace?
Calvin: Too few role models.
-- "Calvin and Hobbes" May 6th, 1990.
As it is, when you read C++ and C# switch statements, you only need to
remember one set of rules.
"Phl Wigglesworth" <phil.wig...@mythlandia.com> wrote in message
news:ehppRMx3AHA.228@tkmsftngp03...