Bill said:
Where, exactly, did you get THAT idea?????
From the online documentation, actually. Here's what it says:
----------------------------------------------------------------------
The switch statement is a control statement that handles multiple
selections by passing control to one of the case statements within its
body. It takes the following form:
switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}
Where:
expression
An integral or string type expression.
statement
The embedded statement(s) to be executed if control is transferred
to the case or the default.
jump-statement
A jump statement that transfers control out of the case body.
constant-expression
Control is transferred to a specific case according to the value of
this expression.
Remarks
Control is transferred to the case statement whose constant-expression
matches expression. The switch statement can include any number of case
instances, but no two case constants within the same switch statement
can have the same value. Execution of the statement body begins at the
selected statement and proceeds until the jump-statement transfers
control out of the case body.
NOTICE THAT THE JUMP-STATEMENT IS REQUIRED AFTER EACH BLOCK, including
the last block whether it is a case statement or a default statement.
Unlike the C++ switch statement, C# does not support an explicit fall
through from one case label to another. If you want, you can use goto a
switch-case, or goto default.
If expression does not match any constant-expression, control is
transferred to the statement(s) that follow the optional default label.
If there is no default label, control is transferred outside the
switch.
----------------------------------------------------------------------
(Emphasis mine.) From that statement, I concluded that you had to have
a break or a goto for each block.
Ironically, today, I see that far down at the bottom of the article,
under the code samples, there's a little block called Code Discussion.
And in it, it includes the following:
----------------------------------------------------------------------
Although fall through from one case label to another is not supported,
it is allowed to stack case labels, for example:
case 0:
case 1:
// do something;
----------------------------------------------------------------------
So I see, as others have pointed out. Until today, I couldn't find the
documentation that said a case could fall through as long *as it
contained no code*. What I did get was compiler errors that said you
couldn't fall through to the next case. In the scenario I was using, I
had a case that I wanted to do one thing, and then fall through to the
next case. Obviously, that's not allowed, but a code-less fall-through
is. It's enough to make me happy.
<CounterRant>
Couldn't you ask the question in a civilized fashion before ASSuming that this was the case?
</CounterRant>
I placed the rant in <RANT> </RANT> so that you'd know that I knew it
was a rant, and to set it off from the civilized portion of the post.
In my *personal* opinion, there's almost always a better way to code
than relying on a goto, and that has (so far) always been true. I'm not
an absolutist; there's an exception to every rule. I was just shocked
that -- based on the documentation in the help files -- it seemed that
they were forcing you to use it.
And I don't think I was assuming anything. I think that I was drawing a
reasonable conclusion based on the information I could find. An
assumption would have been made without having done any research at
all.
switch(student.Grade)
{
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
school = "Elementary";
break;
case "7":
case "8":
case "9":
school = "Junior High";
break;
default:
school = "High School";
break;
}
A little more verbose, but, the same concept.
Agreed. And more than suitable for my purposes. It's actually what I
expected it should look like; but in C, I believe a case can fall
through even if it has code. That was where my confusion came from.
I have to say that I'm not sorry to see that feature gone, tho. I'd bet
it's a source of many subtle bugs.
There was a recent thread on this topic. Feel free to look it up
Sure. Was it in this particular forum? If so, what keywords would you
suggest I use to find the specific thread you have in mind? I'm using
the Google newsreader.
They chose to allow advanced developers the chance to optimize their code when the situation
warranted it, rather than forcing us all to walk "The One True Path".
Bill
That's a reasonable conclusion, given that there is no "One True Path".
I'm just surprised they'd provide you with a construct that encourages
spaghetti code.
Just out of curiosity--did C# drop any keywords that were common to C
and C#? I know that it doesn't support malloc and delete. Are there any
more that were dropped?
Thanks very much for your reply!