Question on switch

I

Ian Tuomi

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
 
E

Ed Morton

Ian said:
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.
 
C

Christopher Benson-Manica

Ian Tuomi said:
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;
}
 
A

Andreas Kahari

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 */
}
 
I

Ian Tuomi

Andreas said:
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
 
A

A. Sinan Unur

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.
 
I

Ian Tuomi

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
 
A

Andreas Kahari

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.
 
M

Mike Wahler

Ian Tuomi said:
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
 
M

Mike Wahler

Ian Tuomi said:
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
 
S

Steve Zimmerman

Ian said:
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
 
D

Dik T. Winter

>
> 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.
 
I

Irrwahn Grausewitz

Dik T. Winter said:
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
 
J

Jack Klein

Oops, ended the comment with /* instead of with */




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Boolean Values 16
How to find out the size of an array? 28
Dont work, it´s something whit the loops? 1
switch case question 9
An unusual use of case/switch 55
What use is void? 23
Standards 14
switch error 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top