473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create 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 2589
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.jyu.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)cyberspace.org |
Nov 13 '05 #3
In article <bk**********@phys-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_FAILURE);
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.jyu.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.jyu.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**********@phys-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

"Ian Tuomi" <ia*******@co.jyu.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.jyu.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*******@freeshell.org> wrote in message
news:sl**********************@sdf.lonestar.org...
In article <bk**********@phys-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**********@phys-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.jyu.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.jyu.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.jyu.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*******@freeshell.org> wrote in message
news:sl**********************@sdf.lonestar.org...
In article <bk**********@phys-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.jyu.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
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
Nov 13 '05 #21
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
Nov 13 '05 #22
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

Nov 13 '05 #23
In article <bk**********@phys-news1.kolumbus.fi> Ian Tuomi <ia*******@co.jyu.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/
Nov 13 '05 #24
"Dik T. Winter" <Di********@cwi.nl> wrote:
In article <bk**********@phys-news1.kolumbus.fi> Ian Tuomi <ia*******@co.jyu.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.
Nov 13 '05 #25
On Wed, 24 Sep 2003 16:41:28 +0000 (UTC), Andreas Kahari
<ak*******@freeshell.org> wrote in comp.lang.c:
In article <bk**********@phys-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_outside_switch */ <- uncomment this line */
yet_another_function();
} <- both cases close here, no break

some_label_outside_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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #26

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

Similar topics

6
1927
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 (...
18
3415
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
1495
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
7399
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
1683
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
8
1263
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...
16
3800
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...
4
1404
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...
17
1601
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
2868
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...
0
7100
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,...
0
6964
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...
0
7126
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,...
1
6842
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...
0
4559
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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...

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.