Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default as first option in switch statement?

I've tested this and it works fine, but it looks... weird... to me. Should I be concerned that this is nonstandard form which will be dropped in a future version of PHP, or that it may stop working? I've always had a default case as the final case, never as the first case...

switch($kind)
{
    default:
        // The kind wasn't valid, set it to the default
        $kind = 'kind1';
        // and fall through:

    case 'kind1':
        // Do some stuff for kind 1 here
        break;

    case 'kind2':
        // do some stuff for kind2 here
        break;

    // [...]

    case 'kindn':
        // do some stuff for kindn here
        break;

}

// some more stuff that uses $kind here...

(In case it's not obvious what I'm trying to do is ensure $kind is valid, hence the default: case. But the switch also performs some operations, and then $kind is used after the switch as well. That's why default: falls through to the first case, and also sets $kind)

Suggestions? Is this normal/valid syntax?

like image 745
Josh Avatar asked Aug 06 '09 21:08

Josh


People also ask

Can default be written first in switch case?

The position of default doesn't matter, it is still executed if no match found. // The default block is placed above other cases. 5) The statements written above cases are never executed After the switch statement, the control transfers to the matching case, the statements written before case are not executed.

What does the default option do in a switch statement?

Hint 1. Adding a default option makes sure that in case your variable doesn't match any of the options, the default will be used.

Is default mandatory in switch statement?

The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.

Is default optional in switch case?

3) The default statement is optional. Even if the switch case statement do not have a default statement, it would run without any problem.


2 Answers

It is an unusual idiom, it causes a little pause when you're reading it, a moment of "huh?". It works, but most people would probably expect to find the default case at the end:

switch($kind)
{
    case 'kind2':
        // do some stuff for kind2 here
        break;

    // [...]

    case 'kindn':
        // do some stuff for kindn here
        break;

    case 'kind1':
    default: 
        // Assume kind1
        $kind = 'kind1';

        break;

}
like image 153
Paul Dixon Avatar answered Oct 01 '22 01:10

Paul Dixon


In case anybody find this page through google as I did:

I was wondering the same thing as Josh - so... One thing is standards, which I think we should all try harder to adhere too, but another thing is hacking (in the: exploit-every-possibility kinda way).

While it's ugly/weird/not normal - it IS possible and IMHO could be useful in some rare cases...

Consider the following:

$color = "greenish";
//$color = "green";

switch($color) {
    default:
        echo "no colors were selected so the color is: ";
    case "red":
        echo "red<br />\n";
        break;
    case "blue":
        echo "blue<br />\n";
        break;
    case "green":
        echo "green<br />\n";
        break;
}

If $color = "greenish"; the code will print

no colors were selected so the color is red

while if $color = "green"; or any other defined cases, it will just print the color.

It know it not the best example, but you get the point ;) Hope it helps somebody.

like image 20
Dan Larsen Avatar answered Sep 30 '22 23:09

Dan Larsen