473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Switch case statement

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
7 3512

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
5
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
14
by: Chris | last post by:
Hi, can you specify a range in a switch - statement ? switch (i) { case 100 - 999 : // do something break; case 1000 - 9999:
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...

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.