1

As an example of lots of ifs 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;
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Malcolm Smith
  • 59
  • 2
  • 6
  • 5
    `else if` should be definitely better as once the condition is true, rest of the evaluations will be skipped. – Harsh Mar 23 '16 at 01:06
  • You don't really mean to have an assignment there at `x=1`, do you? – Ken Y-N Mar 23 '16 at 01:07
  • 2
    Possible duplicate of [Performance difference of "if if" vs "if else if"](http://stackoverflow.com/questions/7970275/performance-difference-of-if-if-vs-if-else-if) – elhefe Mar 23 '16 at 01:19
  • 1
    In terms of actual performance? I'd be surprised if there were any non-negligible difference at all, once JIT kicks in. For style? Definitely `else if`. – Rob Mar 23 '16 at 01:19
  • 1
    @GrantWinney I post [a question on meta](http://meta.stackoverflow.com/questions/319542/possible-duplicate-question-in-another-language) about this "duplicate question" issue. – Cheng Chen Mar 23 '16 at 02:33
  • Regarding my x=1 in the sudo code. Its C# witch I'm working in and it would want me to convert it to type bool so I dont do have if (x=a). I'm using if (variable >0 & variable <= 1) . – Malcolm Smith Mar 25 '16 at 15:48

6 Answers6

5

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.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • What I'm working on has about 200 lines of if statements the longest stretch is 15 consecutive if statements each with condition 1 or condition 2. A max of 30 are asked each time but normally only 6 when there is no user input each also with 2 conditions, and all in a void update loop ruining in C# . – Malcolm Smith Mar 23 '16 at 11:56
  • @MalcolmSmith: could you please include sample code – sujith karivelil Mar 23 '16 at 13:42
2

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.

mxl
  • 97
  • 4
  • Im taking user input for a int box so in my case it would be on average twice as efficient. What I have at the moment works fine but my device is a lot faster than some of the devices that my use it. its 200 lines in a loop 30 of the if runing max each time at the moment. – Malcolm Smith Mar 23 '16 at 12:03
1

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

Richard Quinn
  • 232
  • 1
  • 12
1

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
Ceeee
  • 1,392
  • 2
  • 15
  • 32
1

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.

janeon
  • 59
  • 4
0

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;
}
SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • Thanks I will remember that for future projects its something useful I didn't know about. It would be a lot more time consuming and difficult to change all my current code to use that though. – Malcolm Smith Mar 25 '16 at 15:56