Is it possible to go through multiple cases in one case like this? how?
switch(foo)
{
case 1 or 2:
dosomething(); break; //dosomething if foo is either 1 or 2
case 3:
dosomethingelse (); break; // dosomethingelse if foo is 3
}
--
Ian Tuomi
Jyväskylä, Finland
"Very funny scotty, now beam down my clothes."
GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!
NOTE: Remove NOSPAM from address
Nov 13 '05
25 2649
Ian Tuomi wrote: Oh, thanks! BTW I have the K&R C book but my friend is borrowing it.
Oh, so you HAD K&R. You'll never see it again ;)
Brian Rodenborn
Ian Tuomi wrote: Oh, thanks! BTW I have the K&R C book but my friend is borrowing it.
Oh, so you HAD K&R. You'll never see it again ;)
Brian Rodenborn
Ian Tuomi wrote: Is it possible to go through multiple cases in one case like this? how?
switch(foo) { case 1 or 2: dosomething(); break; //dosomething if foo is either 1 or 2
case 3: dosomethingelse (); break; // dosomethingelse if foo is 3 }
Thank you for your post, Ian.
Yes, it is possible through a technique called "fall through."
switch(foo)
{
case 1:
case 2: dosomething();
break;
...
}
The compiler will automatically execute dosomething() and break for
cases 1 and 2 You can include however many cases you want with this
technique, for example,
switch(foo)
{
case 1: /* cases 1 through 4 "fall through" to case 5 */
case 2:
case 3:
case 4:
case 5: dosomething();
break;
...
}
Cases may also be written horizontally instead of vertically, as
follows.
switch(foo)
{
case 1: case 2: case 3: case 4:
case 5: dosomething();
break;
...
}
--Steve
In article <bk**********@p hys-news1.kolumbus. fi> Ian Tuomi <ia*******@co.j yu.fi> writes: Andreas Kahari wrote: Yes:
switch (foo) { case 1: /* FALLTROUGH /* case 2: bar(); break;
I'm alittle confused now. Looking at that piece of code it looks like in case 1 nothing is done and to access bar() it would require that foo = 2
What am I missing?
You are missing that C differs from (for instance) Pascal. In C
statements are executed after a case until a break is encountered
or the end of the complete switch statement is reached.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
"Dik T. Winter" <Di********@cwi .nl> wrote: In article <bk**********@p hys-news1.kolumbus. fi> Ian Tuomi <ia*******@co.j yu.fi> writes: Andreas Kahari wrote: Yes:
switch (foo) { case 1: /* FALLTROUGH /* case 2: bar(); break;
I'm alittle confused now. Looking at that piece of code it looks like in case 1 nothing is done and to access bar() it would require that foo = 2
What am I missing?
You are missing that C differs from (for instance) Pascal. In C statements are executed after a case until a break is encountered or the end of the complete switch statement is reached.
Right, and for a good (or was it bad?!?) example lookup
question 20.35: What is "Duff's Device"? in the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Regards
Irrwahn
--
Great minds run in great circles.
On Wed, 24 Sep 2003 16:41:28 +0000 (UTC), Andreas Kahari
<ak*******@free shell.org> wrote in comp.lang.c: In article <bk**********@p hys-news1.kolumbus. fi>, Ian Tuomi wrote: Andreas Kahari wrote: switch (foo) { case 1: /* FALLTROUGH /*
Oops, ended the comment with /* instead of with */
I'm alittle confused now. Looking at that piece of code it looks like in case 1 nothing is done and to access bar() it would require that foo = 2
The case doesn't close until it's broken.
There are at least two exceptions:
switch(some_val )
{
case 1:
some_func();
case 2:
some_other_func ();
/* goto some_label_outs ide_switch */ <- uncomment this line */
yet_another_fun ction();
} <- both cases close here, no break
some_label_outs ide_switch:
++some_val;
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Aristotelis E. Charalampakis |
last post by:
Hi all, this is a newbie question :-)
I was wondering if there was a way to use the switch statement in a manner
that each case statement includes more that a simple value. i.e.:
switch ( myFloat )
{
case >0: // ??? how do i write this ???
|
by: Minti |
last post by:
I was reading some text and I came across the following snippet
switch('5')
{
int x = 123;
case '5':
printf("The value of x %d\n", x);
break;
}
|
by: Christopher Benson-Manica |
last post by:
How do you indent your switch statements? My boss prefers
switch(x) {
case 1:
...
case 2:
...
default:
...
}
|
by: William Stacey |
last post by:
Using the following code sample:
public byte Get()
{
// <= Possible to switch Here??
lock(syncLock)
{
//Do something in Get().
}
}
|
by: NotYetaNurd |
last post by:
for(int i=0;i<6;i++)
{
//
}
wont i go out of scope after the for loop ...?
| |
by: mmitchell |
last post by:
I have a thread that does periodic database maintenance. While this
process is going on I would like the rest of the application to hold
off on accessing the database. I thought I could share a property in a
class to hold the status, but that doesn't seem to work.
Any ideas on what the best way for me to determine when the thread is
doing it's work?
Thanks
Mike
|
by: DataPro |
last post by:
New to Sql Server, running SQL Server 2000.
Our transaction log file backups occasionally fail as the size of the
transaction log gets really huge. We'd like to schedule additional
transaction log backups. Does that require an exclusive on the database
or can the db be used during a transaction log backup?
Also, does switching to a bulk mode recovery model before a bulk
operation then switching back to full recovery mode after present...
|
by: Skybuck |
last post by:
Hello,
One of my internet providers has blocked my broadband adsl internet
connection for over a 2 months now. They still steal money from my bank
account and they still hold the adsl line hostage, so I can switch over
to another ADSL internet provider. I could switch over to a (tv) cable
internet provider, however these are more expensive and only few
providers available because of mergers.
I am not happy about all this:
|
by: manuthomas23 |
last post by:
I have a question:
What will be the value of j in the following code? and why is it so?
int i;
for( i = 0; i < 1; i++ )
{
switch( i )
{
case 0: i += 5;
|
by: osward |
last post by:
Hello there,
I am using phpnuke 8.0 to build my website, knowing little on php programing.
I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful.
Here is the 1st problem I encounter:
I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.
|
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...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
| |
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |