473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

switch-statement ???

Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian

Nov 16 '05 #1
14 2321
Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. If
you have only a couple of options per case statement, case can drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...
Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian

Nov 16 '05 #2
"Chris" <ch********@pandora.be> wrote:
switch (i)
{
case 100 - 999 :


That will actually compile, but it won't do what you want. (The minus
sign is subtraction, so it's equivalent to "case -899".)

You can't specify ranges in a C# switch statement.

P.
Nov 16 '05 #3
I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer

On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <mi******@nospam.org>
wrote:
Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. If
you have only a couple of options per case statement, case can drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...
Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian


Nov 16 '05 #4
From 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;

Eran Kampf
http://www.ekampf.com

"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ub********************************@4ax.com...
I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer

On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <mi******@nospam.org>
wrote:
Hi Chris,

We were discussing the heck out of that a few months ago, and the answer
is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient.
If
you have only a couple of options per case statement, case can
drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...
Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian

Nov 16 '05 #5
Also you are aloud to transfer control from from case block to another (which gives the same functionality as fall-through, but in an obviously inteded way rather than happning by accident.

switch(i)
{
case 0:
case 1:
// do some stuff
goto case 2;
case 2:
// do some other stuff
break;
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<uL**************@TK2MSFTNGP11.phx.gbl>

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

Eran Kampf
http://www.ekampf.com

"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ub********************************@4ax.com...
I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer

On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <mi******@nospam.org>
wrote:
Hi Chris,

We were discussing the heck out of that a few months ago, and the answer
is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient.
If
you have only a couple of options per case statement, case can
drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...
Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #6

Also you are aloud to


should be "allowed to"
Nov 16 '05 #7
"Fall-through" is not supported. The correct term is "Stacked case labels".
I apologize for the terminology mix-up.

Thanks,
Michael C., MCDBA

"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ub********************************@4ax.com...
I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer

On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <mi******@nospam.org>
wrote:
Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. Ifyou have only a couple of options per case statement, case can drop-throughlike this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...
Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case statement for every possible value and without using 'if' ?

thanks
Christian

Nov 16 '05 #8
cant we use less than operator ??
case < 100
case <999
??
"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ck**********@sparta.btinternet.com...
"Chris" <ch********@pandora.be> wrote:
switch (i)
{
case 100 - 999 :


That will actually compile, but it won't do what you want. (The minus sign
is subtraction, so it's equivalent to "case -899".)

You can't specify ranges in a C# switch statement.

P.

Nov 16 '05 #9
ambshah <am*****@hotmale.not4spam> wrote:
cant we use less than operator ??
case < 100
case <999


No. You have to provide a single value per case.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
in vb we used this technique a lot its neat

switch(true)

{case (10 < 100):

MessageBox.Show("First ");

break;

case (10> 100):

MessageBox.Show("Second");

break;

default:

MessageBox.Show("default");

break;

}
substitute variable in place of 10

hth
ambrish

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ck**********@sparta.btinternet.com...
"Chris" <ch********@pandora.be> wrote:
switch (i)
{
case 100 - 999 :


That will actually compile, but it won't do what you want. (The minus sign
is subtraction, so it's equivalent to "case -899".)

You can't specify ranges in a C# switch statement.

P.

Nov 16 '05 #11
LOL - I must have been in phonetic mode ;-)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<On**************@TK2MSFTNGP14.phx.gbl>
Also you are aloud to


should be "allowed to"

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.775 / Virus Database: 522 - Release Date: 08/10/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #12
ambshah <am*****@hotmale.not4spam> wrote:
in vb we used this technique a lot its neat


Is that any better than:

if (i < 100)
{
....
}
else if (i > 100)
{
}

though?

If so, why?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Is [VB's Case x To y] any better than
[if i ... else if i ...]
If so, why?


Well, I'll play devil's advocate: the 'if - else if' form makes you
repeatedly specify the variable being tested, which could lead to an
obscure bug if you accidentally specify a different variable at some
point.

P.
Nov 16 '05 #14

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ck**********@hercules.btinternet.com...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Is [VB's Case x To y] any better than
[if i ... else if i ...]
If so, why?


Well, I'll play devil's advocate: the 'if - else if' form makes you
repeatedly specify the variable being tested, which could lead to an
obscure bug if you accidentally specify a different variable at some
point.

P.


On the other hand, if you have the variable mentioned on the top of the
switch and then you have 20 cases spread along four pages of code, you might
just wonder what your case 10-100: means.

Personally, I think that ranged cases allow for more bugs than multiple
if's.

Stefan
Nov 16 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mike and Jo | last post by:
I've been converting some code to C++. I'm trying to use the Switch function to compare a result. Is it possible to use switch to evaluate '>0', '<0', 0? Example switch (result) { case...
5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
13
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the...
13
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
5
by: Nadav | last post by:
Hi, I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
11
by: Smithers | last post by:
Just wondering if the sequence of the case(s) in the switch block might impact performance. Suppose I switch on an int, and I have 4 expected cases. Lets say I expect case 3 to happen 95% of the...
7
by: Chad | last post by:
The program: #include <stdio.h> void hello(void) { printf("hello \n"); } void hi(void) { printf("hi \n");
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.