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;
6
  • 5
    else if should be definitely better as once the condition is true, rest of the evaluations will be skipped.
    – Harsh
    Commented Mar 23, 2016 at 1:06
  • You don't really mean to have an assignment there at x=1, do you?
    – Ken Y-N
    Commented Mar 23, 2016 at 1:07
  • 2
    Possible duplicate of Performance difference of "if if" vs "if else if"
    – elhefe
    Commented Mar 23, 2016 at 1: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
    Commented Mar 23, 2016 at 1:19
  • 1
    @GrantWinney I post a question on meta about this "duplicate question" issue.
    – Cheng Chen
    Commented Mar 23, 2016 at 2:33

6 Answers 6

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.

2
  • 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# . Commented Mar 23, 2016 at 11:56
  • @MalcolmSmith: could you please include sample code Commented Mar 23, 2016 at 13:42
2

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
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.

1
  • 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. Commented Mar 23, 2016 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

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.

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;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.