C# Switch statement

  • Thread starter Duncan McNutt [BSDM]
  • Start date
D

Duncan McNutt [BSDM]

Is it possible to have ranges or patterns in a case ?

If not, why wasnt this designed in to make it easier instead of listing
every case value needed?
 
H

Harry Bosch

Duncan McNutt said:
Is it possible to have ranges or patterns in a case ?

If not, why wasnt this designed in to make it easier instead of listing
every case value needed?

You should get a beginner's C# book and start reading it.
 
D

Duncan McNutt .[BSDM]

I am trying to install MIS 2002 Trial I downloaded from the MS site but I
get the following errors.

How can I fix this and get it installing? Any ideas?

Event Type: Error
Event Source: MsiInstaller
Event Category: None
Event ID: 11720
Date: 8/17/2003
Time: 10:09:38 PM
User: N/A
Computer: VMWARE2KPRO
Description:
Product: Mobile Information Server -- Error 1720.There is a problem with
this Windows Installer package. A script required for this install to
complete could not be run. Contact your support personnel or package vendor.
Custom action IsDCLogon script error -2147023665, : Line 2339, Column 5,
 
S

Stan

Here, I did a search and copied one of the many replies to this common
question.
Yes, like this:

using System;

class Test
{

public static void Main()
{
int x;
x=Convert.ToInt16(Console.ReadLine());
switch (x)
{
case 1:
case 2:
case 3:
Console.WriteLine("Is 1 2 3");
break;
case 4:
case 5:
case 6:
Console.WriteLine("Is 4 5 6");
break;
default:
Console.WriteLine("Is greater than 6");
break;
}
}
}

However, you can't do greater than/less than (i.e. case 4 to 6) like
you could do in VB. You'll have to use if/else statements for that.

'Lib
 
E

Edward Yang

I love C#. But I strongly disagree with M$ C# team on the design of the
switch-break statement.

C# does not allow automatic fall through for each case statement, yet it
requires a mandatory and useless break statement. It's completely insane to
have such a design. The break statement is compeletely NOT NECESSARY!

I understand they want to make programmers make LESS errors by breaking
C/C++ goodness (e.g., range of values). But those errors caused by
case-break pair never get in my way throughout 8 years of C/C++ programming
experience. Actually I've never made a mistake when coding case-break's.

I think they might want to give a compiler warning when there is no break
statement after a case statement. That's the best way keeping C/C++ goodness
yet giving programmers much, much less chance for making errors.

Edward
 
H

Harry Bosch

Michael A. Covington said:
I think they followed C too closely. C, in turn, was designed to be
easy to compile.

It's also because a 'switch' can be optimized a lot better. You are
checking a single value against a series of constants, and code can be
generated to do that which is faster than the code for the corresponding
'if' statements. For instance, the value being checked can be loaded into a
register once, and then checked against the constants, which can be
immediate data in the opcodes.

One could write a compiler to detect that the bunch of 'if's are all
checking a single value against a series of constants, and maybe some do
that (probably C compilers, if any), and thus get code as good as a
'switch'.

Also, when a 'switch' uses continuous case values (no gaps), you can build
a jump table, which is even better. Lots of C compilers do this.

Lexers typically rely on such switch statement optimizations for speed when
they handle the next character in the input stream, as an example where it
pays off. Of course, the compiler doesn't know you're compiling a lexer, it
just generates better code for switches, either way.
 
E

Edward Yang

OMG, I thought it is not allowed to do this!

I read MSDN again, and find this:

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;
Edward

Stan said:
Here, I did a search and copied one of the many replies to this common
question.
Yes, like this:

using System;

class Test
{

public static void Main()
{
int x;
x=Convert.ToInt16(Console.ReadLine());
switch (x)
{
case 1:
case 2:
case 3:
Console.WriteLine("Is 1 2 3");
break;
case 4:
case 5:
case 6:
Console.WriteLine("Is 4 5 6");
break;
default:
Console.WriteLine("Is greater than 6");
break;
}
}
}

However, you can't do greater than/less than (i.e. case 4 to 6) like
you could do in VB. You'll have to use if/else statements for that.

'Lib
 
E

Edward Yang

Sorry, I want to correct a small part of my comment. See Stan's reply in
this thread above this post.

C# does allow the old way of 'stacking' case's.

<msdn>
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; </msdn>

But do not put any code (comment is OK) after case 0 or case 1, otherwise
the non-fall-through compiler error will get you.

Edward

Edward Yang said:
I love C#. But I strongly disagree with M$ C# team on the design of the
switch-break statement.

C# does not allow automatic fall through for each case statement, yet it
requires a mandatory and useless break statement. It's completely insane to
have such a design. The break statement is compeletely NOT NECESSARY!

I understand they want to make programmers make LESS errors by breaking
C/C++ goodness (e.g., range of values). But those errors caused by
case-break pair never get in my way throughout 8 years of C/C++ programming
experience. Actually I've never made a mistake when coding case-break's.

I think they might want to give a compiler warning when there is no break
statement after a case statement. That's the best way keeping C/C++ goodness
yet giving programmers much, much less chance for making errors.

Edward
 
M

Michael Culley

experience. Actually I've never made a mistake when coding case-break's.

Bullshit.
 
E

Edward Yang

1..9 or 1 to 9 seems to be 'clean' code.

But, the real truth is that I think the stack of case's is cleaner code.

And, as a C/C++ programmer, we guard cautiously our religion principles :).
We do not want to borrow something from another religion.

Edward
 
D

Duncan McNutt

C# isnt C/C++, thats the point MC++ is just a strap on for .NET C# was
designed from the ground up for .NET yet they left things as it was, and now
we're gettin partial types and lambada funtions (anonymous methods) expect
spaghetii code now, generic types is the only real biggie comming in 04 that
I can see, the rest is just gonna cause more problems later on I think.
 
E

Edward Yang

Yes, there is no absolute EQUALITY in this world. So C# != C/C++. I quite
agree.

But C# borrows or inherits much of C/C++'s language infrastructures. So what
I said in my previous post is applicable to C#.

BTW, what is Microsoft Product Deactivation Team? Is it funded by M$? :p

Edward
 
P

Peter Vidler

Hi,
but I do NOT remember I ever made a mistake with case-break's.

Maybe YOU don't (doubtful), but many people do. Hence the design decision.

You almost never want a case statement to fall through if there is code in
it.. so enforcing an explicit break or goto makes perfect sense. Isn't it
this same case that was the reason goto was kept in c# (just to move between
cases in switch statements)?

Pete
 
F

Frank Eller [MVP]

Hi Edward,

Edward Yang said:
It is a half yes. See Stan's reply in this thread.

Well, I agree that it's possible to execute a piece of code for more than
one value, but that's not exactly what he meant ... because you have to
write down the "case"-part, which you would not have to do if you could use
a range like in VB.NET. But ok, let's call it a "half yes ..." :)).

REgards,
 
R

Rob Tillie

Deactivate Windows xp's? Deactivate Microsoft? :|
You have certainly no affiliation with Microsoft, just remove it so nobody
gets confused...

GReetz,
-- Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top