Drop through switch case?

In the process of writing a switch message, I read on a PHP site:

Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).

The case with the switch without a break was ideal for what I wanted to do. I want all cases below the agreed to be implemented. Why is this wrong, and what is the best way to do it differently?

Is this bad for all languages ​​or just for PHP? Why?

edit: Is this or not a problem, is there a way to do the same without the case of switching?

+4
source share
3 answers

As noted, even if you might want to avoid their appointment in certain circumstances. As long as you know what you are doing, failing a case is not a problem. Personally, I think this is a big reason why using case arguments can be so useful (instead of && 's, but that's just me).

So the answer is: this is not bad for any particular language, because you are not struggling with the language.

+4
source

Opinions about passing the switch quotes usually reflect opinions about goto.

The switch must be mutually exclusive, but it can be obtained (in any case, C #) using the goto keyword. Since PHP added the goto keyword, you can probably do something like this. An example from MSFT is here in the switch documentation.

Personally, I think it is clearer than clarity regarding the code that I need to execute, which should be mutually exclusive. I found that I am adding / removing cases to switch statements, and therefore have an additional headache to make sure they are properly ordered when using a language that allows a failure.

0
source

In a word: RESOURCES

If you use the IF construct, the interpreter will go to any opportunity to find if it is TRUE or FALSE , this WASTE RESOURCES , Memory and CPU . The whole point of using a SWITCH statement with BREAK is SPARE RESOURCES , because when a matching condition is found, the interpreter will be BREAK and will not try to find more matches.

What you are doing can be easily done using the IF , ELSE, or ELSEIF constructs , rather than using SWITCH if you don't mind spending resources, of course.

-1
source

All Articles