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

Home Posts Topics Members FAQ

Question on switch

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 #1
25 2645
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?

--
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 #2
Ian Tuomi <ia*******@co.j yu.fi> spoke thus:
Is it possible to go through multiple cases in one case like this? how?
Yep...
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
}


switch(foo) {
case 1: case 2:
/* do something */
break;
case 3: default:
/* do something else */
break;
}

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #3
In article <bk**********@p hys-news1.kolumbus. fi>, 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
}

Yes:

switch (foo) {
case 1: /* FALLTROUGH /*
case 2:
bar();
break;
case 3: /* FALLTROUGH /*
case 4: /* FALLTROUGH /*
case 5:
baz();
break;
default:
quux_error();
exit(EXIT_FAILU RE);
break; /* NOTREACHED */
}

--
Andreas Kähäri
Nov 13 '05 #4


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
}


Yes:

switch(foo)
{
case 1: /* fall through */
case 2:
dosomething();
break;
case 3:
dosomethingelse ();
break;
default:
/* handle it */
break;
}

Ed.

Nov 13 '05 #5
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?

--
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 #6
Ian Tuomi <ia*******@co.j yu.fi> wrote in news:bksfmr$bde $1@phys-
news1.kolumbus. fi:
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 a C book that explains how the swicth statment works, and
what a break statement does. Notice that there is no break after case 1:.
Hence, when foo is equal to 1, bar will be called.

Sinan.
Nov 13 '05 #7
You are missing a C book that explains how the swicth statment works, and
what a break statement does. Notice that there is no break after case 1:.
Hence, when foo is equal to 1, bar will be called.

Sinan.


Oh, thanks! BTW I have the K&R C book but my friend is borrowing it.

--
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 #8
Ian Tuomi <ia*******@co.j yu.fi> wrote in news:bksfmr$bde $1@phys-
news1.kolumbus. fi:
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 a C book that explains how the swicth statment works, and
what a break statement does. Notice that there is no break after case 1:.
Hence, when foo is equal to 1, bar will be called.

Sinan.
Nov 13 '05 #9
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.
--
Andreas Kähäri
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1950
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 ???
18
3463
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; }
22
1536
by: Christopher Benson-Manica | last post by:
How do you indent your switch statements? My boss prefers switch(x) { case 1: ... case 2: ... default: ... }
13
7475
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
23
1713
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
8
1277
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
16
3831
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...
4
1428
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:
17
1635
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;
2
2885
osward
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.
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
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...
0
10018
tracyyun
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...
0
9055
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
7553
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
6795
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
5446
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...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
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.