13

Some time ago after not standing anymore lines like this:

if (arg)
    invk(test);
else if (test)
{
    alot();
    stuff();
}

I decided for my self its better for readability in our 1920x1200 times, to not omit the {}.

So I wrote a tool that reformats my existing code.

later I noticed a bug in that tool resulting in

if (x)
{
 ...
}
else if(y)
{
 ...
}
else if(z)
{
 ...
}

had been changed (wihtout obvisiously changing the behavior) into:

if (x)
{
 ...
}
else 
{
    if(y)
    {
     ...
    }
    else
    {
        if(z)
        {
         ...
        }
    }
}

This made me realize (unintended) that this is actually what a else if does by syntax and semantical rules of C.

So is there even an statement like else if() existing or is it just an abuse of semantic that results in this usefull but (lets call it so for this purpose) obfuscation originated wording that breaks any formating rules and just serves as human readable?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
dhein
  • 6,431
  • 4
  • 42
  • 74
  • 4
    No, `else if` is just another `if` following `else`. I think there's a duplicate of this question. – Yu Hao May 04 '16 at 10:27
  • 2
    @MichaelWalz: What you mean by exclusive. I'm not getting why it shouldn't be. But I'd love to get enlighted. – dhein May 04 '16 at 10:32
  • 1
    No, but there is #elif – technosaurus May 04 '16 at 10:35
  • 7
    @technosaurus: This is s nonsense and missleading comment. You confuse core language and preprocessor. – too honest for this site May 04 '16 at 10:37
  • I would not use a *code formatting tool* that inserts `{` between `else` and `if()`. Never. – wildplasser May 04 '16 at 10:49
  • @wildplasser: ofc one should do it never. But I made multiple times clear that this was a bug of my tool, which made me realize this. It never was intended to do so. – dhein May 04 '16 at 10:59
  • @Olaf It is misleading, I've seen new programmers see it in an #ifdef and make the assumption that its semantics can be carried over by removing the # as in #if and #else... usually its the ones with good deductive reasoning skills that make this leap. And BTW the preprocessor _is_ part of the core language "6.10 Preprocessing directives" – technosaurus May 04 '16 at 11:05
  • 1
    @technosaurus: The standard does not use the term "core language". It is commonly the part processed after the preprocessing stages, i.e. after phase 4. – too honest for this site May 04 '16 at 11:09
  • @Zaibis _not exclusive_ : forget this comment, it's stupid, I removed it. – Jabberwocky May 04 '16 at 11:09
  • 1
    @technosaurus I'm afraid people with good reasoning skills who are expecting a rationally-designed, consistent programming language are in for quite a bit of disappointment when it comes to C... – Lundin May 04 '16 at 11:10
  • 1
    The distinction between the preprocessor language (with the `#elif` keyword) and the "core" C language (without a dedicated `else if` keyword) is actually very interesting to make. I see it as a result of C language having exactly 1 statement inside its `if` and `else` clauses, and the preprocessor having several lines inside `#if`, `#elif` and `#else` parts. I guess all languages take one of these two approaches, with Python going one way, Pascal going the other, and C going both ways :) A language-design rule of thumb: if you have some sort of `endif`, you should have some sort of `elseif`. – anatolyg May 04 '16 at 11:55
  • @anatolyg: interesting note. thanks for sharing it. – dhein May 04 '16 at 12:22
  • note that some languages do have an elseif - PHP for example – pm100 May 05 '16 at 01:10
  • 1
    @pm100: Generally languages that use an `end`, `endif`, `end if`, or `fi` marker to terminate an `if` statement need a separate `elsif` or `elseif` keyword to avoid deep nesting. (But PHP doesn't, and `elseif` seems to be equivalent to `else if`.) – Keith Thompson May 05 '16 at 01:48
  • @pm100: note that im actually explicit asking about c – dhein May 05 '16 at 07:08

2 Answers2

18

As per the C11, chapter §6.8.4, selection statements, the available statement blocks are

if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement

and, regarding the else part,

An else is associated with the lexically nearest preceding if that is allowed by the syntax.

So, there is no else if construct, exists as per standard C.

Obviously, an if (or if...else) block can exist as the statement in else block (nested if...else, we may say).

The choice of indentation , is left to the user.


Note:

Just to add, there is no reason for a separate else if construct to exist, the functionality can be achieved by nesting. See this wiki article. Relevant quote

The often-encountered else if in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent "if then else" statements combined with a particular source code layout. However, this also means that a distinct else-if construct is not really needed in these languages.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

As every one is aware, there is several different styles of programming languages. The presence of a elseif is based on the style of language. In a C/Pascal style the if statement is

if (...)
   statment; 
else
   statement;

and you have a compound statement: { statment; statment; }

while in a Modula2 / VB / (python ?) style language you have

if (...)
   statement;
   ...
   statement;
else
   statement;
   ...
   statement;
end-if

in the Modula2 / VB you need a elseif statement because if you tried using else if you get

if (..)
else if (..)
else if (..)
end-if; end-if; end-if;

The end-if's at the end are rather ugly.

As @anatolyg noted this is why you have a #elif in C macro language.


Cobol-85 has a different take on the elseif statement. It provides an extended Select/case statement. In Cobol you have an evaluate:

evaluate option
   when 1
      ...
   when 2
      ...

but you can also do

evaluate true
   when age > 21 and gender = 'male'
      ...
   when age > 21 and gender = 'female'

In Cobol you can also mix the case and if structure

evaluate option also true
   when 1 also age > 21 and gender = 'male'
      .... 
   when 1 also age > 21 and gender = 'female'
      .... 
   when 2 also any
      ....

One final point in java and C you sometimes see

if () {

} else if () {

} else if () {

}

To all intents and purposes, this is VB / Modula-2 written in Java/C.

Bruce Martin
  • 10,358
  • 1
  • 27
  • 38