Switch case statement

S

sam_cit

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);
}
 
C

Chris Dollin

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

Sharath

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.lang.c/browse_thread/thread/39e842db8803cb99/
 
C

CBFalconer

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);
}
 
K

Keith Thompson

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;
}
 
C

Christopher Benson-Manica

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 :)
 
E

Eric Sosman

Christopher Benson-Manica wrote On 01/03/07 12:14,:
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:
 
K

Keith Thompson

Christopher Benson-Manica said:
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.
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top