r/AskComputerScience • u/[deleted] • 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?
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
2
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
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.
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:
Remove all the curly braces above and you end up with:
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.