6

Is there any difference between the following 2 codes functionally? If not, is there a preferred style?

int main()
{
    int i=11;

    if (i > 100)
    {
        i = 100;
    }
    else if (i < 0)
    {
        i = 0;
    }

    cout << i << endl;
}

versus

int main()
{
    int i=11;

    if (i > 100)
    {
        i = 100;
    }
    else if (i < 0)
    {
        i = 0;
    }
    else
    {
    }

    cout << i << endl;
}

In other words, my question is, is there any point in including the else if I don't want it to do anything?

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
David
  • 619
  • 2
  • 8
  • 15
  • 3
    no, there's no difference for adding the "else" in your case, you can just delete the empty else – User2012384 Feb 14 '16 at 04:55
  • 1
    I'm voting to close this question as off-topic because it is of very low quality. – gsamaras Feb 14 '16 at 05:35
  • 1
    There is no functional reason to have an empty `else` block, and the compiler will likely just optimize it away. But, where it does come in handy is including comments that show you understand the flow might reach that block, and maybe someday in the future you might put more logic in that block. – Remy Lebeau Feb 14 '16 at 05:36
  • @gsamaras: there is nothing wrong with the question, it is short and straight to the point, it is fine the way it is. – Remy Lebeau Feb 14 '16 at 05:37
  • 2
    "is there any point in including the else if I don't want it to do anything?" Sure, to show that you have [considered that case and didn't just miss it](http://stackoverflow.com/q/35053371/2756719). – T.C. Feb 14 '16 at 05:37
  • OK @RemyLebeau, I am retracting. – gsamaras Feb 14 '16 at 05:38
  • 1
    @T.C. The difference to that other question is that they place a comment in the `else` clause to document their intent. (Though personally I'd prefer an `assert`ion.) If I see an *empty* `else`, instead of thinking that the author considered but not forget the case, I'd rather think: They knew something had to be done here and wanted to add it later but then forgot. I think it is counter-productive. – 5gon12eder Feb 14 '16 at 05:48
  • I don't have a cite for this, but I vaguely remember that the empty `else` is required by MISRA. – Pete Becker Feb 14 '16 at 14:44
  • An empty else could be useful when you have nested if and for some reason don't want to write braces: `if(cond1) if(cond2) X; else; else Y;`, but that doesn't look like a good reason. – Marc Glisse Feb 14 '16 at 21:46

1 Answers1

9

Significance

To the question:

...does an empty else clause have any significance?

in the context of if { ... } else {} the answer is no. Compilers will probably optimize your else out. Unless you put actual statements (assert, print, error handling) the executable will be virtually identical.


Benefits

To the question:

What are the benefits of an empty else clause in an else if construct?

the answer is discussed at length on this Stack Overflow post. See MISRA publication MISRA C:2012, 15.7 (All if…​else if constructs shall be terminated with an else statement).
It applies to if { ... } else if { ... } else {} construct, not if { ... } else {} construct.


Style

An else { /* no statement */ } in immensely better than an else statement. It does prevent dangling else closures (else not followed by {}) which are downright dangerous since they may mislead a reader of what else actually applies to, and is prone to maintenance errors.

You will find more programming styles1 than you have engineers in a room. May I suggest:

int main() {
    int i = 11;

    if (i > 100) {
        i = 100;
    } else if (i < 0) {
        i = 0;
    }

    cout << i << endl;
}

1 each individual, plus one for the consensus.

manndat
  • 18
  • 4
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • 3
    Frankly, the compiler generating less efficient code would probably be my least concern when I see an empty `else` clause. It is a trivial thing to detect the empty compound statement. Having an empty `else` is just confusing to the human reader. That would be my main concern. – 5gon12eder Feb 14 '16 at 05:43