Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why break?

0 views
Skip to first unread message

Phl Wigglesworth

unread,
May 17, 2001, 12:18:40 PM5/17/01
to
Apart from the argument that C++ uses break statements, is there ever any
point to having them in C#?
The syntax:

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.


Jonathan Allen

unread,
May 17, 2001, 2:12:07 PM5/17/01
to
In order to simulate VB's, Case 1 to 3, you can do this...

(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...

Phl Wigglesworth

unread,
May 17, 2001, 4:48:25 PM5/17/01
to
The syntax is complete - just messy.
My question is why, except to make C++ programmers at home, did they keep
"break" in the language?
It simply does not appear to be needed.

Phil Wigglesworth
Mythlandia, Inc

"Jonathan Allen" <grey...@cts.com> wrote in message
news:OeFzcDw3AHA.2172@tkmsftngp07...

Jonathan Allen

unread,
May 17, 2001, 5:06:35 PM5/17/01
to
> The syntax is complete - just messy.

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...

Thomas Petersen

unread,
May 17, 2001, 5:32:11 PM5/17/01
to
<<
The syntax is complete - just messy.
My question is why, except to make C++ programmers at home, did they keep
"break" in the language?
It simply does not appear to be needed.
>>

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

Phl Wigglesworth

unread,
May 17, 2001, 6:26:07 PM5/17/01
to
The select statement (as shown) is really implemented (something similar
to):
if (x<59) grade = "F";
else if (x<69) grade = "D";
else if (x<79) grade = "C";
else if (x<89) grade = "B";
else if (x<=100) grade = "A";
else throw new Exception("Invalid Grade");

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...

Jonathan Allen

unread,
May 17, 2001, 7:17:46 PM5/17/01
to
> The traditional role of the switch/case statement has
> been to focus the implementation as a lookup table

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...

Weasel

unread,
May 18, 2001, 1:08:52 AM5/18/01
to
> The syntax is complete - just messy.
> My question is why, except to make C++ programmers at home, did they keep
> "break" in the language?
> It simply does not appear to be needed.

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


Marko Bozikovic

unread,
May 18, 2001, 5:24:13 AM5/18/01
to
Phl Wigglesworth wrote:
>
> Apart from the argument that C++ uses break statements, is there ever any
> point to having them in C#?

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.


Eric Gunnerson (MSFT)

unread,
May 18, 2001, 12:09:37 PM5/18/01
to
If your question is why C# has break when there could be an "implicit break"
at the end of each case, the answer is that it would really confuse C++
programmers, and that's who C# is aimed at.

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...

0 new messages