Philosophical discussion of Elif, Elsif, ElseIf

Most languages define ElseIf as a keyword. It makes no sense to me.

Julia:

if <condition>
    ...
elseif <condition>
    ...
else
    ...
end

Python:

if <condition>:
    ...
elif <condition>:
    ...
else:
    ...
end

Bash elif; Ruby elsif; Euphoria elsif; … etc

This has NO advantage in human readability (elsif, elif, elseif are all not in a dictionary). I don’t think the compiler does better optimization with them either.

The following differs from “elseif” in one single space:

if <condition>
    ...
else if <condition>
    ...
else
    ...
end

Why?

2 Likes

The standard answer is that in languages like julia, certain keywords like if introduce a block while others like else and else if continue a block. As a result else if would require an extra end by regularity (also to avoid confusion if you really did want an if block inside your else block). Of course that doesn’t apply to languages that do scoping differently and indeed in e.g. C you’d spell it else if. Python doesn’t really need it either, but as with all things python who knows why it works the way it does.

23 Likes

Even in languages like C where you can just do else if there’s a famous parsing ambiguity due to this construction:

18 Likes

Thank you!

1 Like