473,796 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2356
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********@pan dora.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********@pan dora.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******@nospa m.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********@pan dora.be> wrote in message
news:o2******* *************** *@phobos.telene t-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*****@verizo n.net> wrote in message
news:ub******** *************** *********@4ax.c om...
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******@nospa m.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********@pan dora.be> wrote in message
news:o2****** *************** **@phobos.telen et-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.publi c.dotnet.langua ges.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*****@verizo n.net> wrote in message
news:ub******** *************** *********@4ax.c om...
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******@nospa m.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********@pan dora.be> wrote in message
news:o2****** *************** **@phobos.telen et-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.publi c.dotnet.langua ges.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*****@verizo n.net> wrote in message
news:ub******** *************** *********@4ax.c om...
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******@nospa m.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********@pan dora.be> wrote in message
news:o2******* *************** *@phobos.telene t-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.btint ernet.com...
"Chris" <ch********@pan dora.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*****@hotmal e.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

15
7605
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 (>0): case (<0):
5
3147
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 65536 switch cases. The program would crash when it attempts to run. C++ Compiler does not support above 64K switch cases. I tried to set a limit to 2048 switch cases and 4096 switch cases, but the problem exists when program crashed again. Do...
10
9589
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 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
13
6220
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 microcontroller should display in the LCD the value of the voltage applied to the input of the ADC. The above procedure should only execute once the user has entered "1234" using a keypad that is attached to the 8051 microprocessor.
13
7475
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
3692
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 a context switch? What is the kernel object used for blocking EndRead calls? Event and mutex cause a context switch even when the object is signaled and no wait is needed, usage of critical section prevent this switch from happening... what is the...
11
3160
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 1"); break;
11
10865
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 a better way of doing this? if ("control" == commandString) { } else if ("execute" == commandString)
11
2529
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 time. Would the switch block be expected to execute faster (meaning "start executing the code for case 3 sooner") when case 3 appears first, as opposed to appearing as the 3rd case:, as in the following code? switch (someInt) { case 3:
7
3343
by: Chad | last post by:
The program: #include <stdio.h> void hello(void) { printf("hello \n"); } void hi(void) { printf("hi \n");
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.