About switch case in C, why it does not print out? [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Code before the first ‘case’ in a switch-statement
I have the following set of code in C:
void isFindValue(int value1, int value2)
{
switch (value1)
{
case 1:
printf("value1 is found!\n");
break;
case 2:
printf("value1 is found!\n");
break;
case 3:
switch(value2)
{
printf("aaaaaaaaaaaaa\n");
case 6:
printf("bbbbbbbbbbbb\n");
printf("value2 is found!\n");
break;
}
default:
break;
}
if I call the function as is isFindValue(3,6); the printf of bbbbbbbbbbbb show up, but aaaaaaaaaaaaa does not, why this will happen? should we not do something before the case?

Because switch () works using labels. It jumps to the label of which the condition is satisfied. So when reaching
switch(value2) {
printf("aaaaaaaaaaaaa\n");
case 6:
the control flow immediately jumps to the label case 6: so that it skips the call to printf().
The solution would be placing it correctly, outside of the inner switch:
case 3:
printf("aaaaa\n");
switch (value2) {
etc.

"aaaaaaaaaaaaa" is not showing up because it is misplaced (it's not contained in a label so it is unreachable). You need to move it up to before the second switch statement:
case 3:
printf("aaaaaaaaaaaaa\n");
switch(value2)
{
// body
}

The call to the printf function in the statement :
printf("aaaaaaaaaaaaa\n");
cannot be reached because it is before the first case statement of the second switch.
see Code before the first 'case' in a switch-statement

"aaaaaaaaaaaaa\n" is not being printed within any case. Your code is not properly structured.
Try moving the statement out of the switch block like this:
printf("aaaaaaaaaaaaa\n");
switch(value2)
{
case 6:
printf("bbbbbbbbbbbb\n");
printf("value2 is found!\n");
break;
}
break;

switch always look for case and that's why aaaaaaaaaaaa was skipped. It will execute the statement of related case block.

Related

When we don't use 'break' statement in switch statements,why is 'deafult' evaluated before other cases that is below 'default'?

Yesterday I saw the below code. As you can see it hasn't got any break I predicted that it would print "354" because I thought the default part would be finally evaluated (after checking all the cases.)
But that isn't practically correct as it printed "345". Can somebody explain the reason?
int main ()
{
int a = 2;
switch (2*a-1)
{
case 1: printf ("1");
case 2: printf("2");
case 3: printf("3");
default: printf("4");
case 5: printf("5");
}
}
Since 2*a-1 is 3, the code jumped to the case 3 label and kept running from there. The other labels were ignored because no code ever jumped to them. The default label is only jumped to if the value in the switch doesn't match any of the case labels.

Can this if be added to this switch statement?

Hi so I'm building a hotel management program and I'm trying to make my switch statement short but it's not working. I tried to add a function that had the break inside but I get an error saying break not within a loop or a switch statement:
void goback()
{
char y;
printf("Would you like to go back?(Y/N)");
scanf("%c",&y);
if (y=='Y' || y=='y')
{
break;
}
}
int main(){
do
{
printf (" 1. Add a Room\n 2. Current rooms\n 3. Add a booking\n 4. Current bookings \n 5. Modify a booking\n 6. Print bill\n 7. Exit\n\n");
printf ("Which section would you like to access:");
scanf ("%d",&w);
switch (w){
case 1:
clrscr();
newroom();
goback();
case 2:
clrscr();
roomscan();
goback();
case 3:
clrscr();
addbooking();
goback();
case 4:
clrscr();
currentbooking();
goback();
case 5:
clrscr();
printf("not ready\n");
case 6:
clrscr();
printf("not ready\n");
case 7:
clrscr();
printf("\t\t\t\tLogging out... See you next time!");
exit (1);
break;
default:
printf("try again");
}
}
while (w!=7);
}
of course, you got an error because of the break; statement break you from your goback() function and not from case.
As you know each case block must end with break;.
In your case, you must put the break; statement at the end of each case block and you can make the goback() function returning a boolean to decide if you go back or not, but you must define what to do if the user does not want to go back.
I'm not sure but you might want to add breaks after your statements if they are to be executed separately and not in sequence. here I only see your case 7 contains a break, which means all cases until that one will be executed, if that's what you want it's fine, otherwise maybe try something like this
switch (w){
case 1:
clrscr();
newroom();
goback();
break;
case 2:
clrscr();
roomscan();
goback();
break;
case 3:
...
Here it seems that your goback() function tries to do that break but what it does it's it tries to break from the context where it if called, here an "if" bloc, which can't have a break inside it. That's what the compiler is saying I guess.

switch case in c, default before case

I was trying this :
#include<stdio.h>
int main() {
int i = 2;
switch(i) {
default:{
printf("Hi\n");}
case 1:
printf("Hi1\n");
case 2:
printf("Hi2\n");
}
}
output is "Hi2" as expected, however if i = 3,
#include<stdio.h>
int main() {
int i = 3;
switch(i) {
default:{
printf("Hi\n");}
case 1:
printf("Hi1\n");
case 2:
printf("Hi2\n");
}
}
Output is
"Hi"
"Hi1"
"Hi2"
How does program enter other cases which do not match? I know putting break in default would solve this.
Why this behavior? Is there anything mentioned in C specification for this?
In C (and many other languages) the cases are simply labels that get 'jumped' to. Once execution starts in a selected case, it flows just like normal. If you want execution to 'stop' at the end of a case 'block' you have to use the break statement (or some other flow control statement):
switch(i) {
default:{
printf("Hi\n");}
break;
case 1:
printf("Hi1\n");
break;
case 2:
printf("Hi2\n");
break;
}
}
For whatever it's worth, in my opinion this is was an unfortunate decision made by the language designers since falling through to the next case after execution one or more statements in a case are executed is very, very rarely desired. However, that's the way the language works.
C# addresses this by making it so falling out of a case is illegal - some sort of explicit flow control (a break or a goto) is required at the end of a sequence of of statements in a case (unless it's the last case in the switch).
This is because the code steps through each instruction unless explicitly stated not to.
In a switch() { }, you must be explicit.
Think about the instructions backed by this C. It would be a jump table. Without break, there would be no jump beneath each branch to go after the switch case.
Complementing #alex, try this.
#include<stdio.h>
int main() {
int i = 2;
switch(i) {
default:{
printf("Hi\n");}
case 2:
printf("Hi2\n");
case 1:
printf("Hi1\n");
}
}

Skipping switch cases via false loop is a valid operation?

Would this be legal code or breaking any rules?
switch (expr)
{
do
{
case 6:
/*...*/
if (/*...*/)
break;
case 7:
/*...*/
} while (0);
case 9:
/*...*/
break;
default:
break;
}
Would this be a legal way of executing case 6 followed by case 7 but only if some conditions are met?
Or would this lead into undefined behavior and lets nasal dragons come out of the switch?
p.s. my Question is refering to c99.
EDIT:
What i want to do is the following:
assume, case 9, has to be executed in everycase. If expr is 6, i have to execute 6, under some conditions 7 and after that 9 or if expr is 7 its 7->9
so i just want to skip 7 if some conditions are met, but i cant change the order of 6,7,9.
EDIT2:
I'm not looking for an alternative soloution, I'm just interested in the behavior of this snippet.
If a switch statement has an associated case or default label within the scope of an
identifier with a variably modified type, the entire switch statement shall be within the
scope of that identifier.
from ISO/IEC 9899:TC3 6.8.4.2->3
Lets me feel unsure about its behavior.
But I'm not sure that would aim code like mine snippet too.
Whilst I believe the above code is valid, I would make it more explicit:
case 6:
...
/* fall through */
case 7:
if (expr != 6 || condition_to_run_7)
{
do whatever 7 does;
}
if (should_not_do_9)
case 9:
...
break;
}
Alternatively, which is the cleaner solution: move what the 6, 7 and 9 cases does into individual functions, and call the relevant functions (more than once, if needed) in the switch-statement, and have proper "break" after each case...
Maybe I'm one of those lousy programmers. I'm not sure about the standards, but I've been doing things like that when it's needed. That said, if I wanted to do what you have stated in your question, I'd do something like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int iCnt, iTmp;
for(iCnt = 1; argc > iCnt; iCnt ++)
{
iTmp = (int) strtol(argv[iCnt], NULL, 10);
switch(iTmp)
{
case 6:
printf("Case 6\n");
/* change this condition to whatever suits you */
if (argc - 1 == iCnt)
{
case 7:
printf("Case 7\n");
}
case 9:
printf("Case 9\n");
default:
printf("Default case\n");
}
printf("\n");
}
return 0;
}
i.e. an if statement is more intrinsic here IMO.
Valid yes; doing what you want? I doubt it.
Assuming the breaks you have put in are the only ones, then assuming the compiler doesn't get into a complete state (not implausible frankly, especially if you have optimisations turned up), then the following will happen
case 9:: runs its block and continues
default: runs its block and continues
case 7 : runs its block repeatedly forever
case 6 : run's its block, maybe 7, repeats forever
To do what you want, remove the do..while, and put the if block at the end of the 6 around the 7, possibly putting a guard to check whether you are in the 6 or 7 case.
switch (expr)
{
case 6:
/*...*/
break;
case 7:
if ( expr==7 || !/*...*/)
{
/*...*/
}
case 9:
/*...*/
break;
default:
break;
}
However, it's not how I'd do it. This is probably one where wrapping it into child functions would make sense:
void handle6() { /*...*/ }
void handle7() { /*...*/ }
void handle9() { /*...*/ }
switch (expr)
{
case 6:
handle6();
if( /*...*/ ) handle7();
handle9();
break;
case 7:
handle7();
case 9:
handle9();
break;
default:
/*...*/
}
is another way which is probably clearer.
and for completeness...
switch( expr )
{
case 6:
/*...*/
if( /*...*/ ) goto case_9;
case 7:
/*...*/
case 9:
case_9:
/*...*/
break;
default:
break;
}

is it possible to do an OR in a case statement?

I want to do something like:
case someenumvalue || someotherenumvalue:
// do some stuff
break;
Is this legal in C?
The variable that I am doing a switch on is an enumerated list data struct.
You can rely on the fact that case statements will fall-through without a break:
case SOME_ENUM_VALUE: // fall-through
case SOME_OTHER_ENUM_VALUE:
doSomeStuff();
break;
You can also use this in a more complicated case, where both values require shared work, but one (or more) of them additionally requires specific work:
case SOME_ENUM_VALUE:
doSpecificStuff();
// fall-through to shared code
case SOME_OTHER_ENUM_VALUE:
doStuffForBothValues();
break;
Yes, you can simply skip the break in the first case to achieve the fall-through effect:
switch (expr) {
case someenumvalue: // no "break" here means fall-through semantic
case someotherenumvalue:
// do some stuff
break;
default:
// do some other stuff
break;
}
Many programmers get into the trap of fall-through inadvertently by forgetting to insert the break. This caused me some headaches in the past, especially in situations when I did not have access to a debugger.
you need:
case someenumvalue:
case someotherenumvalue :
do some stuff
break;
You can use fallthrough to get that effect:
case someenumvalue:
case someotherenumvalue :
do some stuff
break;
A case statement like a goto -- your program will execute everything after it (including other case statements) until you get to the end of the switch block or a break.
As others have specificied, yes you can logically OR things together by using a fall through:
case someenumvalue: //case somenumvalue
case someotherenumvalue : //or case someothernumvalue
do some stuff
break;
But to directly answer your question, yes you can do a logical, or bit-wise or on them as well (it's just a case for the result of the operation), just be careful that you're getting what you'd expect:
enum
{
somenumvalue1 = 0,
somenumvalue2 = 1,
somenumvalue3 = 2
};
int main()
{
int val = somenumvalue2; //this is 1
switch(val) {
case somenumvalue1: //the case for 0
printf("1 %d\n", mval);
break;
case somenumvalue2 || somenumvalue3: //the case for (1||2) == (1), NOT the
printf("2 %d\n", mval); //case for "1" or "2"
break;
case somenumvalue3: //the case for 2
printf("3 %d\n", mval);
break;
}
return 0;
}
If you choose to do the second implementation keep in mind that since you're ||'ing things, you'll either get a 1 or 0, and that's it, as the case.

Resources