473,324 Members | 2,193 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,324 software developers and data experts.

switch without breaks

I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?

I wrote a program to demonstrate how a switch without breaks behaves
vs. how I expected it to behave. The code includes:
(1) a switch statement with breaks
(2) the if/else statements that have the same results as (1)
(3) a switch statement without breaks
(4) the if/else/goto statements that have the same results as (3)
(5) the if statements that I thought would have the same results as (3)

Below is the code, followed by the output of the program when it's
called with "-b".

#include <stdio.h>

main(int argc, char **argv)
{
int c;

c = getopt(argc, argv, "abcd");
if(c == EOF) {
fprintf(stderr, "ERROR: At least one option must be
used.\n");
exit(1);
}

printf("\n1. Using switch with breaks:\n");
switch(c) {
case 'a':
printf("option a!\n");
break;
case 'b':
printf("option b!\n");
break;
case 'c':
printf("option c!\n");
break;
case 'd':
printf("option d!\n");
break;
default:
printf("wrong option!\n");
}

printf("\n2. Using if/else:\n");
if( c == 'a' ) {
printf("option a!\n");

} else if( c == 'b' ) {
printf("option b!\n");

} else if( c == 'c' ) {
printf("option c!\n");

} else if( c == 'd' ) {
printf("option d!\n");

} else {
printf("wrong option!\n");
}

printf("\n3. Using switch without breaks:\n");
switch(c) {
case 'a':
printf("option a!\n");
case 'b':
printf("option b!\n");
case 'c':
printf("option c!\n");
case 'd':
printf("option d!\n");
default:
printf("wrong option!\n");
}
printf("\n4. Using if/else/goto:\n");
if( c == 'a' ) {
goto a;
} else if( c == 'b') {
goto b;
} else if( c == 'c') {
goto c;
} else if( c == 'd') {
goto d;
}

a: printf("option a!\n");

b: printf("option b!\n");

c: printf("option c!\n");

d: printf("option d!\n");

printf("wrong option!\n");

printf("\n5. Using if:\n");
if( c == 'a' ) {
printf("option a!\n");
}
if( c == 'b' ) {
printf("option b!\n");
}
if( c == 'c' ) {
printf("option c!\n");
}
if( c == 'd' ) {
printf("option d!\n");
}
printf("wrong option!\n");
}

=====================

1. Using switch with breaks:
option b!

2. Using if/else:
option b!

3. Using switch without breaks:
option b!
option c!
option d!
wrong option!

4. Using if/else/goto:
option b!
option c!
option d!
wrong option!

5. Using if:
option b!
wrong option!

Jan 11 '06 #1
10 12285

"Evie" <ev****@verizon.net> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?


Because the evaluation only happens in the 'switch' term,
and it only happens once.

int main(void)
{
int x = 2;

switch(x) /* evaluate 'x' */
/* if x == 1, goto case 1 */
/* if x == 2, goto case 2 */
/* any other value, goto default */
{
case 1:
case 2: /* execution will jump to here. */
/* 'x' will not be evaluated again */

default:
}

return 0;
}
-Mike
Jan 11 '06 #2
Evie <ev****@verizon.net> wrote:
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated? (3) a switch statement without breaks
(4) the if/else/goto statements that have the same results as (3)
(5) the if statements that I thought would have the same results as (3)


These should tell you everything you wanted to know about switch -
as you can see, it's essentially a glorified goto. That's why 4 and
not 5 duplicated the effects of a switch without breaks. What would
be the point of checking subsequent cases anyway?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jan 11 '06 #3
In article <11*********************@g43g2000cwa.googlegroups. com>,
Evie <ev****@verizon.net> wrote:
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?
Your model of switch statements is wrong. The cases aren't tests
which are checked as they are encountered, they are labels indicating
where to start processing. A switch behaves just like your version 4:
printf("\n4. Using if/else/goto:\n");
if( c == 'a' ) {
goto a;
} else if( c == 'b') {
goto b;
} else if( c == 'c') {
goto c;
} else if( c == 'd') {
goto d;
}


so it's not surprising that it produces the same output!

-- Richard
Jan 11 '06 #4
Evie wrote:
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?
Because
a) case labels are just that -- labels
b) the numbers for the cases have to be compile time constants.
I wrote a program to demonstrate how a switch without breaks behaves
vs. how I expected it to behave. The code includes:
(1) a switch statement with breaks
(2) the if/else statements that have the same results as (1)
(3) a switch statement without breaks
(4) the if/else/goto statements that have the same results as (3)
See my correction below.

Cheers
Michael
(5) the if statements that I thought would have the same results as (3)

Below is the code, followed by the output of the program when it's
called with "-b".

#include <stdio.h>

main(int argc, char **argv)
{
int c;

c = getopt(argc, argv, "abcd");
if(c == EOF) {
fprintf(stderr, "ERROR: At least one option must be
used.\n");
exit(1);
}

printf("\n1. Using switch with breaks:\n");
switch(c) {
case 'a':
printf("option a!\n");
break;
case 'b':
printf("option b!\n");
break;
case 'c':
printf("option c!\n");
break;
case 'd':
printf("option d!\n");
break;
default:
printf("wrong option!\n");
}

printf("\n2. Using if/else:\n");
if( c == 'a' ) {
printf("option a!\n");

} else if( c == 'b' ) {
printf("option b!\n");

} else if( c == 'c' ) {
printf("option c!\n");

} else if( c == 'd' ) {
printf("option d!\n");

} else {
printf("wrong option!\n");
}

printf("\n3. Using switch without breaks:\n");
switch(c) {
case 'a':
printf("option a!\n");
case 'b':
printf("option b!\n");
case 'c':
printf("option c!\n");
case 'd':
printf("option d!\n");
default:
printf("wrong option!\n");
}
printf("\n4. Using if/else/goto:\n");
if( c == 'a' ) {
goto a;
} else if( c == 'b') {
goto b;
} else if( c == 'c') {
goto c;
} else if( c == 'd') {
goto d;
}
You forgot the default:

goto my_default;

a: printf("option a!\n");

b: printf("option b!\n");

c: printf("option c!\n");

d: printf("option d!\n");

my_default:
printf("wrong option!\n");

printf("\n5. Using if:\n");
if( c == 'a' ) {
printf("option a!\n");
}
if( c == 'b' ) {
printf("option b!\n");
}
if( c == 'c' ) {
printf("option c!\n");
}
if( c == 'd' ) {
printf("option d!\n");
}
printf("wrong option!\n");
}

=====================

1. Using switch with breaks:
option b!

2. Using if/else:
option b!

3. Using switch without breaks:
option b!
option c!
option d!
wrong option!

4. Using if/else/goto:
option b!
option c!
option d!
wrong option!

5. Using if:
option b!
wrong option!

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 11 '06 #5
Michael Mair wrote:
Because
a) case labels are just that -- labels
b) the numbers for the cases have to be compile time constants.


Reason (b) explains Christopher Benson-Manica's question "What would
be the point of checking subsequent cases anyway?" If the numbers for
the cases could be variables, there would be a point in checking their
values at run time.

Thanks, everyone. I now understand how it works.

- Evie

Jan 11 '06 #6
Evie wrote:
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?


One reason is that you can't have multiple values as part of a case. As
others have said, they are just labels.

So:

switch (val)
{
case 1,2,3:
/* do stuff */
}

Is NOT valid. So how do you get one that triggers on any of those three
values?

switch (val)
{
case 1:
case 2:
case 3:
/* do stuff */
}

Brian

Jan 11 '06 #7
Also,

#include <stdio.h>
#define THIS 1
#define REASON 1
int main(void)
{
int sw;
sw = REASON;
switch(sw)
{
case THIS:
printf("Case 1");
REASON;
case 2:
printf("Case 2");
break;
default:
printf("Default 1");
break;
}
return 0;
}
....and I think two defaults should compile.

Jan 12 '06 #8
"ballpointpenthief" <Ma*************@gmail.com> writes:
Also,

#include <stdio.h>
#define THIS 1
#define REASON 1
int main(void)
{
int sw;
sw = REASON;
switch(sw)
{
case THIS:
printf("Case 1");
REASON;
case 2:
printf("Case 2");
break;
default:
printf("Default 1");
break;
}
return 0;
}
...and I think two defaults should compile.


Sorry, I don't understand what point you're making here. Your
"REASON;" is eqivalent to "1;"; it does nothing. The program
will print

Case 1Case 2

because the first case falls through to the second (newlines would be
helpful).

Are you saying that it should be legal to have two "default:" labels
in a single switch statement? Why? What would you expect that to do?

--
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 12 '06 #9
Whoops, my bad.
I meant to put an example with two equivalent labels.
(and it turns out that that doesn't work.)

Jan 12 '06 #10
ballpointpenthief wrote:

Whoops, my bad.
I meant to put an example with two equivalent labels.
(and it turns out that that doesn't work.)


What, if anything, are you talking about? Include adequate
context. For assistance in doing this on the broken Google Usenet
interface, see my sig. below. Be sure to read the referenced URL
before posting again.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 12 '06 #11

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

Similar topics

5
by: Martin Lucas-Smith | last post by:
Is there any need to keep the final break in a switch which uses a default at the end? I.e: switch ($data) { case 'foo': # Action break;
3
by: eric rudolph | last post by:
I am writing a photo gallery and suppose 8 photos are displayed. When the user clicks on a button under the picture, I want it to add that picture name to a "favorites" list within the session...
14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
4
by: Andrea Williams | last post by:
I used to program with VBS and I'm used to allowing multiple cases in one CASE line. Anyway to do that C#? VB Example: Select Case (lngStatus) Case 1,2,3 'do something Case 4 'do something
24
by: Mark | last post by:
Hi - how can I get a switch statement to look for a range of values? I have: function payCalc(field, field2) switch(field.value) { case 0-50: field2.value="lower band"; case 51-99:...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
16
by: Silent1Mezzo | last post by:
Is it possible to have multiple control variables in a switch statement? For example: switch(a , b) { case(a>b): case(b<a): case(a==b): }
27
by: sinbad | last post by:
how to implement a switch case for stringse...switch should happen depending on the strings... thanks sinbad
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.