473,569 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch statement: Is it possible to include something like "Case var > 5" in a case statement?

Thanks,
Juan.
Nov 16 '05 #1
5 2213
No, it has to be a constant integral or string expression. What you can do
it the following

int myInt = ...;

switch (myInt)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// Do something here for 0 <= myInt < 5
break;
case 5:
case 6:
// Do something here for myInt = 5 or 6
break;
}

something like that.

Hope this helps.

"Juan" <ju************ *****@ANTISPAMh otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Thanks,
Juan.

Nov 16 '05 #2
No. But you can do:

switch (n)
{
case 1:
case 2:
case 3:
case 4:
DoFoo();
break;
case 5:
DoBar();
break;
}

Note that fallthrough is only allowed if the case block is empty, otherwise
you have to state a "break" for every "case".

"Juan" <ju************ *****@ANTISPAMh otmail.com> schrieb im Newsbeitrag
news:#X******** ******@TK2MSFTN GP12.phx.gbl...
Thanks,
Juan.

Nov 16 '05 #3
Thanks, got it.

"cody" <de********@gmx .de> escribió en el mensaje
news:#5******** ******@TK2MSFTN GP10.phx.gbl...
No. But you can do:

switch (n)
{
case 1:
case 2:
case 3:
case 4:
DoFoo();
break;
case 5:
DoBar();
break;
}

Note that fallthrough is only allowed if the case block is empty, otherwise you have to state a "break" for every "case".

"Juan" <ju************ *****@ANTISPAMh otmail.com> schrieb im Newsbeitrag
news:#X******** ******@TK2MSFTN GP12.phx.gbl...
Thanks,
Juan.


Nov 16 '05 #4
Thanks, got it.

"Jako Menkveld" <ja***********@ envalue.ch> escribió en el mensaje
news:eJ******** ******@TK2MSFTN GP12.phx.gbl...
No, it has to be a constant integral or string expression. What you can do it the following

int myInt = ...;

switch (myInt)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// Do something here for 0 <= myInt < 5
break;
case 5:
case 6:
// Do something here for myInt = 5 or 6
break;
}

something like that.

Hope this helps.

"Juan" <ju************ *****@ANTISPAMh otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Thanks,
Juan.


Nov 16 '05 #5
Juan,

I thought I'd point out the "why" behind the "no, you can't".

In C, C++, Java, and now C#, the switch / case construct is intended as
a high-speed choice between multiple alternatives. Some languages (such
as COBOL) allow conditions as tests for each of the branches, which is
still a "choice between multiple alternatives," but it's slow. If you
were allowed to do something like this:

switch (myInt)
{
case > 0 && < 5:
DoSomething();
break;
case >= 5:
DoSomethingElse ();
break;
default:
Error();
break;
}

Then the compiler would have little choice but to generate code that
tested the conditions one-by-one until it found one that matched. (I'll
leave aside the semantics of what to do if more than one condition were
true.) In other words, the compiler would have to generate the same
code as if you had said:

if (myInt > 0 && myInt < 5)
{
DoSomething();
}
else if (myInt >= 5)
{
DoSomethingElse ();
}
else
{
Error();
}

Instead, the switch...case in the C/Java school is intended to be
compiled into a high-speed lookup. The compiler does _not_ generate
code to test one case after another, in sequence, until it finds the
matching one. (That is, unless it's a profoundly stupid compiler.)
Instead, it generates a rapid lookup scheme to find the correct "case"
branch in the minimum amount of time. There are, of course, many
strategies, from binary searches through the case list to hash tables
of pointers to the code to run. Regardless, the idea is that in a
switch statement with 200 cases you don't have to test up to 200 times
in order to find out what to do.

The only optimization of this structure I've seen was in Pascal, which
allowed you to say

case 1..15:

as a shortcut for repeating

case 1:
case 2:
....
case 15:

but it still didn't allow you to put full-blown boolean conditions in
the "case" statements, for the above reasons.

Nov 16 '05 #6

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

Similar topics

26
14121
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the appropriate document, or post an example? I don't relish the idea especially long if-else statements. Joe
12
3210
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
2
15627
by: Tomas Larsson | last post by:
Hi. I have some problems when using a static readonly declaration of a guid in a switch/case statement. I'll give you an example. public sealed class Activites { private Activites(){}
15
3500
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
6
1927
by: graeme g | last post by:
hi how would i write the following in switch case statement: if (x < 40) y = 0; else if (x < 65) y = 1; else if (x < 80) y = 2;
7
3525
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include <stdio.h>
6
4530
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
5
2881
by: DaemonCoder | last post by:
I have a switch/case statement using numerics for a menu, but when i enter an alphanumeric statment it goes into a loop printing my menu and the default statement from switch/case the default statement. The only way to end it is ctrl-c. the code is below. Can someone please explain this quirk to me? #include<stdio.h> #include<stdlib.h> int...
13
11789
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size...
0
7698
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...
0
7612
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...
0
8122
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...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
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...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
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...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.