Switch vs if statements

后端 未结 6 984
無奈伤痛
無奈伤痛 2020-12-17 10:15

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

switch ($x) 
{
case 1:
  //mysql query 
  //echo something
  break;
case 2:
  //mysql query 
         


        
相关标签:
6条回答
  • 2020-12-17 10:25

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

    0 讨论(0)
  • 2020-12-17 10:30

    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 )

    0 讨论(0)
  • 2020-12-17 10:33

    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.

    0 讨论(0)
  • 2020-12-17 10:45

    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.

    0 讨论(0)
  • 2020-12-17 10:45

    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 讨论(0)
  • 2020-12-17 10:47

    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.

    0 讨论(0)
提交回复
热议问题