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.
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.
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.
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.
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 appropriate document, or
post an example? I don't relish the idea especially long if-else
statements.
Joe
|
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 <stdio.h>
|
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 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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |