552 views
0 votes
0 votes
In the switch statement, if we write default case above all then will it be executed?

a = 2;

switch(a){

default : print("abc");

case 2 : print("xyz");

}

what will be the o/p ?

2 Answers

1 votes
1 votes
Case and the default clause can occur in any order. And default clause is also optional. According to your program output will be "xyz"
1 votes
1 votes
If the switch matches with some case, then the default will not be executed. (provided every case has its break statement )

but if none of the cases matches with switch variable then the default will be executed.

In this case, you have the default in the top and with no break statement. That will cause case 2 to execute also.

Here output will be xyz because case is matched.

but if a=3, then output will be abcxyz
edited by

No related questions found