top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the potential risk if we dont have the default case in the switch statement?

+1 vote
355 views
What is the potential risk if we dont have the default case in the switch statement?
posted Nov 26, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Use of default case in switch statement is to provide an option to do something if any of the case is not matched.

So in simple world its nothing but a else part of nested if, else if,

if (condition1) {
} else if (condition2) {
} else if (condition3) {
....
.... //so on
} else {
}

If non of the condition matched then it will falls into the else condition. Like wise in

switch(expression) {
     case exp1:
         //do something
          break; /*optional */
     case exp2:
         //do something
          break; /*optional */
     ...
     ...
     case expN:
         //do something
          break; /*optional */
     default:
           /* If non of the above case satisfied then do this */
           // What ever you want to do here, if the non of the case matches it is upto the programer
           // what he wanted to do, like providing a info how to use OR anything else.
}
answer Nov 26, 2014 by Arshad Khan
0 votes

No Risk At All...

But, If value/expression inside the switch() doesn't satisfy any of the case then what it has to do??
So,, If you write default, then it is nothing but you are adding another case,, which is telling that if none of your case satisfied then this to be done..

answer Dec 10, 2014 by Chirag Gangdev
...