r/AskComputerScience • • Oct 20 '21

Why can no programming language agree on 'else if'?

It seems every language I learn has a different way of doing it:

- else if

- elif

- elsif

- elseif

etc.

What's the story behind this and why did every language decide to implement it differently?

59 Upvotes

19 comments sorted by

77

u/Cybyss Oct 20 '21 edited Oct 20 '21

In C, C++, Java and related languages there really is no "else if". It's just an "else" immediately followed by an "if" while leaving off the optional curly braces. For example:

if (something)
{
    print("hello")
}
else
{
   if (another_thing)
   {
       print("goodbye")
   }
}

Remove all the curly braces above and you end up with:

if (something)
    print("hello")
else if (another_thing)
    print("goodbye")

Other languages can't make use of this grammar hack (e.g., for example Python using indentation instead of optional curly braces to denote code blocks), so they have to have a dedicated keyword for "else if". Nobody has agreed on what this keyword should be yet.

26

u/shawmonster Oct 20 '21

Lol I didn't realize this until I took a compilers course. blew my mind that there was no actual "else if" statement in the C family.

0

u/[deleted] Oct 20 '21

[deleted]

15

u/dnswblzo Oct 20 '21
if (a) {
    printf("A");
}
else if(b) {
    printf("B");
}
else if(c) {
    printf("C");
}

is equivalent to

if (a) {
    printf("A");
}
else {
    if(b) {
        printf("B");
    }
    else {
        if(c) {
            printf("C");
        }
    }
}

14

u/Rangsk Oct 21 '21

As a not-so-fun consequence to this, if you have too many else if statements in a row, you will hit the compiler's nested block limit.

11

u/nemec Oct 21 '21

3

u/Rangsk Oct 21 '21

It was "parsing" an ini and yes, it was very ugly.

0

u/[deleted] Oct 20 '21

[deleted]

2

u/Wingels Oct 20 '21

It's been a little while for me but I think it's more "statement" than it is "line". Every if/else without the block can execute a single statement, and the second if/else would all equate to being part of that if statement. So something like: if () else Is all the same if statement, whereas: if () else if () else Would be the first if statement, and the nested if statement. But the entire nested statement is part of the first statement's "else" block

2

u/dnswblzo Oct 20 '21

Most of the time lines are meaningless to compilers in C-style languages, they are used so that humans can understand the code. You don't need curly braces after an else if there is only one statement in the body, not one line. An if statement and its body is a considered a single statement, so an if after an else is a single statement, and does not need to be in curly braces.

1

u/shawmonster Oct 20 '21 edited Oct 20 '21

I'm not exactly sure what you're asking. The first example you provided isn't possible, because the compiler expects some statement to come after an if clause. If you immediately provide an "else" after your "if", it is a parsing error (because the word "else" itself isnt a statement).

The grammar for if-then-else statements look something like:

if a then s
if b then s1 else s2

The "a" and "b" in this example are just conditional expressions (the things that go between parentheses).

"s" "s1" and "s2" are statements. In C family languages, a statement can be thought of as anything that ends in a semicolon, or things like a for loop, while loop, switch case, etc.. (or even another if statement!). Another type of statement is a Block statement, which is just a list of the statements I previously named surrounded by curly braces.

The reason there are 2 lines in the grammar for if statements is because there are two options. Either you write

if (cond) stmt

or you write

if (cond) stmt1 else stmt2

The important thing to note is that the "stmt2" after the "else" could be another if statement. Thus creating the "else if" construct that we're used to.

I'm not entirely sure what you're asking because in both examples you provided, there would be a parser error. You didn't provide a stmt after the if (cond), you immediately provided an "else". The parser isn't expecting this, so your program won't compile.

As an example, I wrote a short C program that attempts to compile what you provided.

#include <stdio.h>

int main(void) {
    int x = 1;
    if (x < 0) else { printf("Hello World\n"); }
}

When trying to compile this program, I get the error

test.c:5:13: error: expected expression
        if (x < 0) else { printf("Hello World\n"); }
                   ^
1 error generated.

23

u/NoNotTheDuo Oct 20 '21

Let me answer your question with a question.

Why do (spoken) languages have different words for blue?

  • Blue

  • Azul

  • Blau

  • Bleu

You're going to find that different programming languages have different syntax for everything. What's important is knowing when to use the specific commands/controls/structures - the syntax is easy to look up.

13

u/wjrasmussen Oct 20 '21

IF all programming languages did everything the same way, we would have only one programming language.

10

u/chuby1tubby Oct 20 '21

ELSE IF all programming languages did everything differently, we would have far too many programming languages.

-1

u/wjrasmussen Oct 20 '21

SO what if programming languages were dollar you have. Would you say far too many dollars?

4

u/ghjm MSCS, CS Pro (20+) Oct 20 '21

Different requirements and aesthetic preferences over time. In the early days, file sizes really mattered a lot, and languages tended to be more ad-hoc. Today file size is barely a concern, and languages try to have a consistent philosophy for why things are the way they are.

Most "new" languages don't even have a separate elseif statement - they just have a convention that you're allowed not to indent a second if after an else.

2

u/danpietsch Oct 20 '21

Interesting and related reading: The Dangling else .

2

u/[deleted] Oct 20 '21

It's implemented differently because it's of no consequence, so no one's ever made a convincing argument for one way being best, or for there being a standard across languages.

1

u/midwayfair Oct 20 '21

Just to add to a couple other answers: Python specifically implemented it as elif because 4 spaces is the recommended tab length for Python. It's possible that others were chosen for similar reasons.

1

u/[deleted] Oct 22 '21

I suggest:

if
ef
ef
el

1

u/Nathan1123 Nov 18 '21

Programmers are inherently lazy and hate verbosity (source: I am one), so they can't stand having to write two words when one will do. Exactly how to combine "else if" into one word was not something anyone agreed on.