how to use switch case for a range of number
-
Saturday, August 01, 2009 3:37 PM
As above... could this statement work ? case (0 - 500)public void Switch(int num) { switch (num) { case (0-500): // belong to 0-500; break; case (501-1000): // belong to 501-1000; break; } }
All Replies
-
Saturday, August 01, 2009 3:48 PMHave you tried it? Posting takes longer than testing in the IDE.
-
Saturday, August 01, 2009 3:50 PM
You can't really do that with a switch.
You can do:
if (num >= 0 && num <= 500) { ........}
else if (num >= 501 && num <= 1000) {.............}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Proposed As Answer by Atondo_Luis Sunday, August 02, 2009 7:34 PM
- Marked As Answer by Roahn LuoModerator Tuesday, August 04, 2009 5:01 AM
-
Saturday, August 01, 2009 4:00 PM
You can do it with small ranges of numbers like so:
switch (myInt) { case 0: case 1: case 2: // do something break; etc... }But I definately wouldn't recommend that for 500 of em.- Marked As Answer by Roahn LuoModerator Tuesday, August 04, 2009 5:01 AM
-
Saturday, August 01, 2009 4:00 PMJohnWein, that doesn't work in C# unfortunately. The switch only deals with constants
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com -
Saturday, August 01, 2009 4:01 PM
The answer is NO!
See the same question and answer here:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c46bee82-ac9a-4f15-bcc0-9e5baa557916/
VB can do it but not C#. People suggested using if/else.
Oh, by the way, (0 - 500) is an expression and
(0 - 500) = -500
John Chen -- See my team blog: http://blogs.msdn.com/vsdata- Marked As Answer by Roahn LuoModerator Tuesday, August 04, 2009 5:01 AM
-
Saturday, August 01, 2009 4:06 PM
JohnWein, that doesn't work in C# unfortunately. The switch only deals with constants
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
What doesn't work in C#? You can certainly write code that doesn't work in the IDE. Try it. -
Saturday, August 01, 2009 4:13 PM
Read the documentation here on switch
switch (expression)
{
case constant-expression :
statement
jump-statement
[default:
statement
jump-statement]
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com -
Saturday, August 01, 2009 4:36 PM
Read the documentation here on switch
switch (expression)
{
case constant-expression :
statement
jump-statement
[default:
statement
jump-statement]
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
If this post is for my benefit, read my original post in this thread. The OP should have been able to answer his question without posting it to a forum. If you need assistance start a new thread. -
Saturday, August 01, 2009 4:59 PM
Hi,
After I see all this answers there is no comment they are right you can't, so the best solution With such large ranges it is easier to use if - else if statements.Thanks
We are volunteers, if the reply help you mark it as your answer. thanks!!
My Blog- Marked As Answer by Roahn LuoModerator Tuesday, August 04, 2009 5:01 AM
-
Saturday, August 01, 2009 5:08 PMIt wasn't directed at you JohnWein, it was meant for all, generally speaking.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com -
Saturday, August 01, 2009 5:24 PMModerator
As long as it is a range with a constant interval, you can map the range to an integer:
int range = (num-1) / 500;
switch (range) {
case 0: break; // 1-500
case 1: break; // 501-1000
// etc...
}
if/else for non-constant intervals.
Hans Passant.- Marked As Answer by Roahn LuoModerator Tuesday, August 04, 2009 5:00 AM

