5 2200
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*****************@ANTISPAMhotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Thanks, Juan.
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*****************@ANTISPAMhotmail.com> schrieb im Newsbeitrag
news:#X**************@TK2MSFTNGP12.phx.gbl... Thanks, Juan.
Thanks, got it.
"cody" <de********@gmx.de> escribió en el mensaje
news:#5**************@TK2MSFTNGP10.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*****************@ANTISPAMhotmail.com> schrieb im Newsbeitrag news:#X**************@TK2MSFTNGP12.phx.gbl... Thanks, Juan.
Thanks, got it.
"Jako Menkveld" <ja***********@envalue.ch> escribió en el mensaje
news:eJ**************@TK2MSFTNGP12.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*****************@ANTISPAMhotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... Thanks, Juan.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: junky_fellow |
last post by:
Which is better using a switch statement or the if-then equivalent
of switch ?
|
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(){}
|
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);
|
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;
|
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...
|
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)
|
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...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |