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

Home Posts Topics Members FAQ

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 1934
"graeme g" <gg*****@vrms.c omwrote in message
news:uK******** ********@TK2MSF TNGP05.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.WriteLi ne(String.Forma t("{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.WriteLi ne(string.Forma t("{0}, {1}", x, y));

}

"graeme g" <gg*****@vrms.c omwrote in message
news:uK******** ********@TK2MSF TNGP05.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.ne twrote in message
news:%2******** ********@TK2MSF TNGP03.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.c omwrote in message
news:uK******** ********@TK2MSF TNGP05.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.Leng th; 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
1380
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
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 //////// //////////////////////////////////////////
4
2505
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
15729
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
7746
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 to 9 in the menu of options: /*the next method to be implemented is the method presentResultsToUser() which is expected to print a menu of options on the screen and also will ask the user to choose from that menu*/ public void...
67
7400
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 constantly cursing at the goto statement, accusing it of being some sort of spawn of satan. So it seems we have a pickle here. The thing is, at first thought it seems that there aren't any viable alternatives which are better suited for this...
10
2878
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 of non- working code: CvMat *rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images- //rowVect is a pointer to CvMat type cvReshape( images, rowVect, 0, 1 ); //vectorize the image; readrow is a 1xN matrix(vector)
0
1599
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 addresses as SIP URI for communication. I am using IRTCClientProvisioning::GetProfile() IRTCClientProfile::EnablePrsenceEx() IRTCClientProvisioning::EnableProfileEx()
7
3364
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 parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
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
10244
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...
1
10201
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
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...
1
7558
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
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.