1

Not a problem I'm having, just a thought I had and had no idea.

Assume you want to check through numerous possibilities, for example:

if(x > 1000)
{
    return 1000;
}
if(x > 990)
{
    return 1010;
}
if(x > 980)
{
    return 33;
}
if(x > 970)
{
    return 2;
}
if(x > 960)
{
    return null;
}

In that situation, I could also use else if's instead of just entering a new if, and it would have the exact same effect. Is there a performance difference at all? If there is, what is the "more correct" way to do it?

pingu2k4
  • 956
  • 2
  • 15
  • 31

3 Answers3

3

Usually no. In normal cases if do not return in each statement if/else if/else is better because in this construction if you find one true condition, you can stop looking at the remaing if blocks.

But in your case, there is not difference.

Plínio Pantaleão
  • 1,229
  • 8
  • 13
1

Regarding performance, this could conceivably vary based on compiler, but in general there should be no difference. See answer here:

Performance difference of "if if" vs "if else if"

In terms of which is more correct, that is entirely dependent on intent and logic involved. In your example above, it's perfectly correct to just use "if" (no "else") because each condition early exits, and there is no possibility of ambiguity. However, if your conditions did not early exit, then a single value of x could match multiple conditions. So correctness is based on whether the logic in each condition should be compounded so that for an x with a certain value, all the logic in each matching condition should be applied. Or, whether only one set of logic should run in response to the first condition met (in that case, you would have to use "else if"). I have never encountered any other reason to choose between "if" and "else if" other than intent and whether each condition is distinct and non-overlapping, or cumulative in effect.

Community
  • 1
  • 1
Daniel Hall
  • 13,457
  • 4
  • 41
  • 37
0

I ran some test code using the Stopwatch class and it appears that else ifs are slightly faster than multiple ifs. The difference is only a matter of a few ticks

private void button1_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
        sw.Start();
        Bar(1337);
        sw.Stop();

        Console.WriteLine("Else ifs: " + sw.ElapsedTicks);

        sw.Reset();
        sw.Start();
        Foo(1337);
        sw.Stop();

        Console.WriteLine("Multiple Ifs: " + sw.ElapsedTicks);
}

private bool Foo(int x)
    {
        if (x > 1000000)
            return true;

        if (x > 100000)
            return true;

        if (x > 10000)
            return true;

        if (x > 1000)
            return true;

        if (x > 100)
            return true;

        if (x > 10)
            return true;

        if (x > 1)
            return true;

        return false;
    }

    private bool Bar(int x)
    {
        if (x > 1000000)
            return true;
        else if (x > 100000)
            return true;
        else if (x > 10000)
            return true;
        else if (x > 1000)
            return true;
        else if (x > 100)
            return true;
        else if (x > 10)
            return true;
        else if (x > 1)
            return true;
        else
            return false;
    }

Note: C#.NET Code example

Tyler Benzing
  • 1,116
  • 11
  • 25
  • I wonder why that is, because I can't see a reason why they would be different, but also have no evidence to suggest that is definitely the case either. I wonder if someone is able to explain why this may be the case? – pingu2k4 Apr 11 '16 at 22:22
  • @MatthewParker No clue, as some have stated it is dependent on the compiler – Tyler Benzing Apr 11 '16 at 22:28