简体   繁体   中英

is it more efficient to use multiple if statements or one with lots of else ifs after it?

As an example of lots of if s and many else if after one if at the start. I have added some pseudo code below.

if (x=1)
  print x;
if (x=2)
  print x;
if (x=3)
  print x;

Or

if (x=1)
  print x;
else if (x=2)
  print x;
else if (x=3)
  print x;

Your code won't compile; if you really want to check for conditions you need to use == instead for = ; more than efficiency, these two techniques are used depending on the requirements. You can use the first case for the following scenario:

if (x==1)
 //Do something
 // 'x' may change here
if (x==2) // check for next condition
 // Do another thing
 // 'x' may change
if (x==3) // check for another condition
 // Do something more
 //'x' may change

You can do the second set of code if the value for x is not changing. So that you can skip evaluating the rest of conditions once a true condition is found. Consider x=1 So it won't check for x==2 and x==3 so that we can reduce the execution time.

x=1;
if (x==1) // Evaluates to true;
  // Do Something
  // Exit the if
else if (x==2) // will not check for this
  //Do another thing
else if (x==3) // will not check for this
  //Do another thing

If you have more entries to check you should use a switch instead of these two.

It is more efficient to use "if else" because if the condition is true, it won't check the other "if"s. But in your first structure, it doesn't matter which "if" is ture still it checks all other if statements.

Efficency really depends on the compiler/ compiler version you are running. But unless you are doing thousands of if or if else performance wont really be affected. I would go with if else as when the condition is true the rest of the if statements dont need to be run

Both are efficient and both have their own different use.

Using If else if . For example if the requirement or problem is finding if a number is odd or even:

$num = 0

if($num == 0)
   // print 'neutral'
else if($num % 2 == 0)
   // print even
else
  // print odd

output:

neutral

As you can see in the above example, If one condition satisfy, we do not need to compare it to other condition for we already have the answer, its waste of resources and will yield incorrect result if we do :

$num = 0

if($num == 0)
   // print neutral
if($num % 2 == 0)
   // print even
if($num % 2 != 0)
  // print odd

output :

neutral
even

Using Ifs . Another real life example might be if you are comparing 3 numbers and checking which one is largest:

$num1 = 2
$num2 = 30
$num3 = 31

$large = $num1    

if($num2 > $large)
     $large = $num2
if($num3 > $large)
     $large = $num3

output:

largest is $num3 or 31

as you can see in the above example, we need to compare all of the numbers to get the largest. If we do this we will yield incorrect result:

$num1 = 2
$num2 = 30
$num3 = 31

$large = $num1    

if($num2 > $large)
     $large = $num2
else if($num3 > $large)
     $large = $num3

output:

largest is $num2 or 30

In most cases, using if-elseif-else and switch statements over if-if-if statements is more efficient, since it makes it easier for the compiler to create jump/lookup tables. It is also better practice since it makes your code more readable, and the compiler makes sure you include a default case in the switch. This answer, along with this table comparing the three different statements was synthesized using other answer posts on this page as well as those of a similar SO question .

Even better - use a switch case :

int x = 3;

switch (x)
{
    case 1:
        print "1";
        break;
    case 2:
        print "2";
        break;
    case 3:
        print "3";
        break;
    default:
        print "something else";
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM