473,805 Members | 2,027 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
25 2648

"Ian Tuomi" <ia*******@co.j yu.fi> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...
Is it possible to go through multiple cases in one case like this? how?

switch(foo)
{
case 1 or 2:
case 1:
case 2:
dosomething(); break; //dosomething if foo is either 1 or 2

case 3:
dosomethingelse (); break; // dosomethingelse if foo is 3
}


Remember that if there's no 'break' on a 'case
clause', execution falls through to the next.
So just 'stack' case values which should do
the same thing, as above.

-Mike
Nov 13 '05 #11
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 #12

"Ian Tuomi" <ia*******@co.j yu.fi> wrote in message
news:bk******** **@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?


case 1:

// nothing to do here, continue to next statement

case 2:

Only a 'break' statement after case '1' and before
case '2' will cause case '2' to be skipped.

-Mike
Nov 13 '05 #13

"Andreas Kahari" <ak*******@free shell.org> wrote in message
news:sl******** **************@ sdf.lonestar.or g...
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.


Just like detective work. :-)

-Mike

Nov 13 '05 #14
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 #15

"Ian Tuomi" <ia*******@co.j yu.fi> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...
Is it possible to go through multiple cases in one case like this? how?

switch(foo)
{
case 1 or 2:
case 1:
case 2:
dosomething(); break; //dosomething if foo is either 1 or 2

case 3:
dosomethingelse (); break; // dosomethingelse if foo is 3
}


Remember that if there's no 'break' on a 'case
clause', execution falls through to the next.
So just 'stack' case values which should do
the same thing, as above.

-Mike
Nov 13 '05 #16
Ian Tuomi <ia*******@co.j yu.fi> wrote in
news:bk******** **@phys-news1.kolumbus. fi:

You are missing a C book that explains how the swicth statment works,
and
[snip]
Oh, thanks! BTW I have the K&R C book but my friend is borrowing it.


Get it back, fast.

--
- Mark ->
--
Nov 13 '05 #17

"Ian Tuomi" <ia*******@co.j yu.fi> wrote in message
news:bk******** **@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?


case 1:

// nothing to do here, continue to next statement

case 2:

Only a 'break' statement after case '1' and before
case '2' will cause case '2' to be skipped.

-Mike
Nov 13 '05 #18

"Andreas Kahari" <ak*******@free shell.org> wrote in message
news:sl******** **************@ sdf.lonestar.or g...
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.


Just like detective work. :-)

-Mike

Nov 13 '05 #19
Ian Tuomi <ia*******@co.j yu.fi> wrote in
news:bk******** **@phys-news1.kolumbus. fi:

You are missing a C book that explains how the swicth statment works,
and
[snip]
Oh, thanks! BTW I have the K&R C book but my friend is borrowing it.


Get it back, fast.

--
- Mark ->
--
Nov 13 '05 #20

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

Similar topics

6
1952
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
3466
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
1716
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
3832
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
2887
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
9716
marktang
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...
0
10356
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...
1
10361
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,...
0
10103
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...
1
7644
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
6874
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
5536
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
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3006
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.