E
Evie
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!
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!