Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch statement without break

How come a case option in a switch statement that does not contain a break automatically forwards to a next case without check?

try {
    switch($param) {
        case "created":
            if(!($value instanceof \DateTime))
                throw new \Exception("\DateTime expected, ".gettype($value)." given for self::$param");
        case "Creator":
            if(!($value instanceof \Base\User)) {
                throw new \Exception(get_class($value)." given. \Base\User expected for self::\$Creator");                  
            }
        default:
            $this->$param = $value;
            break;
    }
} catch(Exception $e) {
    echo $e->getMessage();
}

If the param is "created" it will do the check in the created-case, which is good. When the check is succesful, I want the code to continue to the default option, that's why there is no break;. But instead it continues to "Creator" while $param != "Creator"!

I do know how to solve this (just add the default code in my case "created"), but I don't like to repeat that code too often. My actual question is: Why does it continue with the "Creator" case while the case is not "Creator".

like image 535
Rene Terstegen Avatar asked Nov 11 '10 14:11

Rene Terstegen


People also ask

Can Switch case be used without break?

Break will return control out of switch case.so if we don't use it then next case statements will be executed until break appears. The break statement will stop the process inside the switch.

What happens if a switch statement has no break?

Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached. This continuation may be desirable in some situations. The default statement is executed if no case constant-expression value is equal to the value of expression .

Are breaks necessary in switch?

When you switch on a value, the switch statement essentially does a goto to the label with the matching value. This means that the break is necessary to avoid passing through to the code under the next label.


1 Answers

Fallthrough was an intentional design feature for allowing code like:

switch ($command) {
  case "exit":
  case "quit":
    quit();
    break;
  case "reset":
    stop();
  case "start":
    start();
    break;
}

It's designed so that execution runs down from case to case.

default is a case like any other, except that jumping there happens if no other case was triggered. It is not by any means a "do this after running the actual selected case" instruction. In your example, you could consider:

  switch($param) {
    case "created":
        if(!($value instanceof \DateTime))
            throw new \Exception("\DateTime expected, ".gettype($value)." given for self::$param");
        break;
    case "Creator":
        if(!($value instanceof \Base\User)) {
            throw new \Exception(get_class($value)." given. \Base\User expected for self::\$Creator");                  
        }
        break;
}

$this->$param = $value;

The rule of thumb here is, if it doesn't depend on the switch, move it out of the switch.

like image 109
Victor Nicollet Avatar answered Sep 19 '22 18:09

Victor Nicollet