By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,125 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,125 IT Pros & Developers. It's quick & easy.

Switch case statement

P: n/a
Hi Everyone,

In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?

Thanks in advance

#include <stdio.h>

int main()
{
int i = 1;
switch(i)
{
case 2: printf("before...\n");
break;
case 4:
case 5:
case 6: printf("in...\n");
break;
case 7: printf("out...\n");
break;
}
return(0);
}

Jan 2 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a

sa*****@yahoo.co.in wrote:
Hi Everyone,

In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?

Thanks in advance

#include <stdio.h>

int main()
{
int i = 1;
switch(i)
{
case 2: printf("before...\n");
break;
case 4:
case 5:
case 6: printf("in...\n");
break;
case 7: printf("out...\n");
break;
}
return(0);
}

Not better way unless you use any compiler provided extensions to give
range in the case statement.
Read:
http://groups.google.com/group/comp....842db8803cb99/

Jan 2 '07 #2

P: n/a
sa*****@yahoo.co.in wrote:
Hi Everyone,

In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?
No.

Also: they're not /values/, they're /statements/. It isn't a "single
case": it's three /case labels/.

What problem do you fear here?
#include <stdio.h>

int main()
{
int i = 1;
switch(i)
{
case 2: printf("before...\n");
break;
case 4:
case 5:
case 6: printf("in...\n");
break;
case 7: printf("out...\n");
break;
}
return(0);
}
This body is best written

return 0;

since, `i` being `1`, the switch doesn't do anything at all ...
one of the hazards of on-the-fly examples.

--
Chris "hopefully not Pyecroft" Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Jan 2 '07 #3

P: n/a
sa*****@yahoo.co.in wrote:
>
In the following code, i have a common action for three switch
cases, is there any other better way to write the three values
in a single case?

Thanks in advance

#include <stdio.h>

int main()
{
int i = 1;
switch(i)
{
case 2: printf("before...\n");
break;
case 4:
case 5:
case 6: printf("in...\n");
break;
case 7: printf("out...\n");
break;
}
return(0);
}
Yes :-)

#include <stdio.h>

int main(void) {
int i;

for (i = 1; i < 8; i++) {
switch(i) {
case 2: printf("before"); break;
case 4:
case 5:
case 6: printf("in"); break;
case 7: printf("out"); break;
default: printf("nada"); break;
}
printf(" %d\n", i);
}
return(0);
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 2 '07 #4

P: n/a
sa*****@yahoo.co.in writes:
In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?

#include <stdio.h>

int main()
{
int i = 1;
switch(i)
{
case 2: printf("before...\n");
break;
case 4:
case 5:
case 6: printf("in...\n");
break;
case 7: printf("out...\n");
break;
}
return(0);
}
No, there's no standard syntax for specifying a range in a case
statement (yes, it might be nice if there were). For only 3 cases,
enumerating each case is probably the best approach. For a larger
range, say a few hundred cases, you can just use a series of if/else
statements. The following is equivalent to your program, but can
easily be modified to cover larger ranges:

#include <stdio.h>
int main(void)
{
int i = 1;
if (i == 2) {
printf("before...\n");
}
else if (i >= 4 && i <= 6) {
printf("in...\n");
}
else if (i == 7) {
printf("out...\n");
}
return 0;
}

If you have a combination of individual values and large ranges, you
can even combine a switch statement with an if statement:

#include <stdio.h>
int main(void)
{
int i = 1;
switch (i) {
case 2:
printf("before...\n");
break;
case 7:
printf("out...\n");
break;
default:
if (i >= 4 && i <= 6) {
printf("in...\n");
}
break;
}
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 2 '07 #5

P: n/a
sa*****@yahoo.co.in wrote:
In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?
Sure!

#include <stdio.h>

#define CASE3(x) case x: case x+1: case x+2:

int main(void) {
int i=1;
switch(i) {
case 1:
printf( "1\n" );
break;
CASE3(2) /* From 2 to 4 */
printf( "2 to 4\n" );
break;
default:
printf( "default\n" );
}
return 0;
}

The implementation of the CASE100 macro is left as an exercise :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 3 '07 #6

P: n/a
Christopher Benson-Manica wrote On 01/03/07 12:14,:
sa*****@yahoo.co.in wrote:

>In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?


Sure!

#include <stdio.h>

#define CASE3(x) case x: case x+1: case x+2:

int main(void) {
int i=1;
switch(i) {
case 1:
printf( "1\n" );
break;
CASE3(2) /* From 2 to 4 */
printf( "2 to 4\n" );
break;
default:
printf( "default\n" );
}
return 0;
}

The implementation of the CASE100 macro is left as an exercise :-)
#define CASE100(n) CASE64(n) CASE36((n)+64)
#define CASE64(n) CASE32(n) CASE32((n)+32)
#define CASE36(n) CASE32(n) CASE4((n)+32)
#define CASE32(n) CASE16(n) CASE16((n)+16)
#define CASE16(n) CASE8(n) CASE8((n)+8)
#define CASE8(n) CASE4(n) CASE4((n)+4)
#define CASE4(n) CASE2(n) CASE2((n)+2)
#define CASE2(n) case n: case (n)+1:

--
Er*********@sun.com
Jan 3 '07 #7

P: n/a
Christopher Benson-Manica <at***@ukato.freeshell.orgwrites:
sa*****@yahoo.co.in wrote:
> In the following code, i have a common action for three switch cases,
is there any other better way to write the three values in a single
case?

Sure!

#include <stdio.h>

#define CASE3(x) case x: case x+1: case x+2:

int main(void) {
int i=1;
switch(i) {
case 1:
printf( "1\n" );
break;
CASE3(2) /* From 2 to 4 */
printf( "2 to 4\n" );
break;
default:
printf( "default\n" );
}
return 0;
}

The implementation of the CASE100 macro is left as an exercise :-)
The OP asked for a *better* way to write it; I don't think your
solution qualifies.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.