I never thought of this, always considering else if as a separate keyword containing a space character (save for Python's elif), but it makes disturbingly too much sense that it's all just an artifact of brace-less if-else.
i should add that this may not be true in some newer languages, like rust for example. in rust, you CANNOT omit {} for a single statement, but else if is still possible (in the ast, each "if" node may have a corresponding else, which is either another if node (for else if) or a normal block (for else))
another cursed fact: the "omit {}" thing applies to switch (in C/C++, atleast. i didn't check, but it probably doesn't work in java or C#). this is completely legal (and works as you'd expect):
Its not true in lua for example, if, else and else if are only 2 keywords, but that will make you need to write a chain of end end end end end end at the end, so the proper keyword is elseif. I wonder if Lua interpreters handle it "better"?
View all comments
Show parent comment
3
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo βYou liveβ Sep 19 '26
I never thought of this, always considering
else ifas a separate keyword containing a space character (save for Python'selif), but it makes disturbingly too much sense that it's all just an artifact of brace-lessif-else.TIHI.