开发者

Switch vs if statements

开发者 https://www.devze.com 2023-01-26 00:45 出处:网络
I\'m in a dilemma. Which is best to use and why.. switch or if? switch ($x) { case 1: //mysql query //echo somethin开发者_如何学运维g

I'm in a dilemma. Which is best to use and why.. switch or if?

switch ($x) 
{
case 1:
  //mysql query 
  //echo somethin开发者_如何学运维g
  break;
case 2:
  //mysql query 
  //echo something
  break;
}

...

if ($x == 1) {
    //mysql query 
    //echo something    
} 

if ($x == 2) {   
    //mysql query 
    //echo something
}  


They have different meanings.

The first example will stop when the condition is met.

The second will test $x twice.

You may want to make your second if an else if. This will mean the block will be skipped as soon as one condition evaluates to true.

But if you are wondering which one is fastest, you should instead think which one most effectively communicates what I want to do. They will both most probably end up as conditional jumps in the target architecture.

Premature optimisation... (you know the rest :P )


Switch is better when there are more than two choices. It's mostly for code readability and maintainability rather than performance.

As others have pointed out, your examples aren't equivalent, however.


The switch statement will be faster because it only checks the value once.


First what you have is not the same

switch(value)
  case condition1:
    break;
  case condition2:
    break

if (condition1) {
}
if (condition2) {
}

These are synonymous

switch(value)
  case condition1:
    break;
  case condition2:
    break

if (condition1) {
}
else if (condition2) {
}

Second if you are talking about my second example where the two are synonymous. Then using switch statements can ease some pain in coding lots of if ..else if statements....

Switches in my point of view can also provide a bit more readability. But even then there are times when using if...else is simply better especially with complex logic.


If you have a single variable, a set of possible values, and you want to perform different actions for each value, that's what switch-case is for.

Switch statements make it more obvious that you are merely determining which of the allowed values a variable had.

If you have more complex conditions, or multiple variables to consider, use if-statements.


both of the statement are decision making statement by comparing some sort of the parameters and then show the results. the switch statement is no doubt faster than the if statement but the if statement has a bigger advantage over the switch statement that is when you have to use logical operations like (<,>,<=,>=,!=,==). whenever you came across with the logical operations you will be struck off by using switch statement but don't worry there is another way which can be use to reach at your goal that is if-statement or if-else or if-else-if statement. I'll suggest you to use if-else most of the time rather than the switch one.

0

精彩评论

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