开发者

Is there any "Else Case" in the switch function to use?

开发者 https://www.devze.com 2022-12-20 05:01 出处:网络
A swi开发者_开发知识库tch statement consists of \"cases\"... But is there any \"else\" case for all other cases?

A swi开发者_开发知识库tch statement consists of "cases"...

But is there any "else" case for all other cases?

Have never found the answer to this...

ex:

 switch ($var){
   case "x":
       do stuff;
   break;
   case "y":
       do stuff;
   break;
   else: // THIS IS WHAT I WOULD LIKE
       do stuff;
   break;
 }


default:
       do stuff;
break;

Typically the default clause should be at the very end of your other case clauses for general readability.

You may also want to reformat your break statements in your code to look like this:

 switch ($var){
   case "x":  // if $var == "x"
       do stuff;
       break;
   case "y":  // if $var == "y"
       do stuff;
       break;
   default:  // if $var != "x" && != "y"
       do stuff;
       break;
 }

Extra information on the switch statement available here and here.


As Dan said but in complete form if it helps...

switch ($var) {
    case "x":
        // do stuff
        break;
    case "y":
        // do stuff
        break;
    default:
        // do "else" stuff...
}


As the others said. Not though that default might also be at the beginning or somewhere wildly in the middle:

switch (foo) 
{
case 0:  break;
default: break;
case 1:  break;
};

Surely you should not do this if not justified.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号