473,405 Members | 2,338 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,405 software developers and data experts.

Easy Question using switch / case

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;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;

is it easy or not possible?

thanks
g
Oct 10 '06 #1
6 1920
"graeme g" <gg*****@vrms.comwrote in message
news:uK****************@TK2MSFTNGP05.phx.gbl...
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;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;
It's not possible, I'm afraid. Case statements use equality.

///ark
Oct 10 '06 #2
is it easy or not possible?

Not possible.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 10 '06 #3

Hmmm.

I couldn't figure out a C# version.
But for kicks, I tried to write a VB.net version, and it worked.

Private Sub SelectCaseTest()

Dim x As Int32 = 11

Dim y As Int32 = 0

Select Case x

Case Is < 10

y = 11

Case Is < 20

y = 21

Case Else

y = 999

End Select

Console.WriteLine(String.Format("{0}, {1}", x, y))

End Sub



Hmmmmmmmmmmmm.

This was all I could come up with, but I didn't try too long: But its not a
solution I know.

private static void SelectCaseTest()

{

int x = 11;

int y = 0;

switch (x)

{

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

case 10:

y = 11;

break;

default:

y=999;

break;

}

Console.WriteLine(string.Format("{0}, {1}", x, y));

}

"graeme g" <gg*****@vrms.comwrote in message
news:uK****************@TK2MSFTNGP05.phx.gbl...
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;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;

is it easy or not possible?

thanks
g


Oct 10 '06 #4
"sloan" <sl***@ipass.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I couldn't figure out a C# version.
I don't think it's possible because the case statement doesn't accept
operators, only absolute values...
But for kicks, I tried to write a VB.net version, and it worked.
Another one to add to the list of differences :-)
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
y = 11;
Not much good if x <= 0 :-)
Oct 10 '06 #5
graeme g wrote:
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;
else if (x < 90)
y = 3;
else if (x < 96)
y = 4;
else
y = 5;

is it easy or not possible?

thanks
g
Hi Graeme,

Unfortunately, switch statements only work across constant values so there's
no easy way to represent those statements. However, if you think about it,
it's more concise using the if..else if statements you have already, since
a case statement *must* include a 'break;', meaning you will be adding an
extra line of code to each condition.

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 10 '06 #6
"graeme g" <gg*****@vrms.comwrote in message
news:uK****************@TK2MSFTNGP05.phx.gbl...
hi

how would i write the following in switch case statement:
[snipped]
For what it's worth (in addition to the good comments you've received
already), it seems to me that what you're doing would be easily (and maybe
more elegantly) handled using an array to define the mapping. For example:

int [] mpivalinum = new int[] {40, 65, 80, 90, 96};
int y;

for (y = 0; y < mpivalinum.Length; y++)
{
if (x < mpivalinum[y])
{
break;
}
}

It has more lines of code for a few numbers, but about the same for the
handful in your example, and a lot less as the number of cases goes up.

Pete
Oct 11 '06 #7

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

Similar topics

4
by: Robert Scheer | last post by:
Hi. I am trying to use switch this way, without success: //thedata is a numeric variable switch (thedata) { case < 10: ... case < 20: ...
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 ...
4
by: google | last post by:
<html> <head> <title>JavaScript Replace</title> <script language="JavaScript1.1" type="text/javascript"> function phils_image_price(input) { //declare variables var output="" var position=0
2
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(){}
1
by: Thiero | last post by:
Hi I posted s thread but did have any reply, I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
0
by: srinivas srinivas | last post by:
Hi, I am developing simple peer-peer RTC application for monitoring the SDP packets and i need to set the TLS security for the transport. But iam struggling to achieving this. Iam using IP...
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.