Listen, I know this is going to seem crazy to some of you, but the else if keyword doesn’t exist in Java. Yeah, I was today years old when I learne...
For further actions, you may consider blocking this person and/or reporting abuse
Maybe this counts as mind blowing as well. The
elif
if python does not work any differently than theelse if
in Java. The only valid reason for having thiselif
construct is the formatting. In Python you would have to use correct spacing when usingelse if
There is no difference in program flow or even speed when you use an
elif
construct. The blocks are just arranged in a nicer way. Very much like, when in Java you are usinginstead of
The solution in your case to get 'nicer looking' code without the usage of
else
so much is something like:P.S. What is wrong with you Americans? Don't you care for the alphabet? What happened to 'E'?
Maybe I was unclear, but the reason that this is so mind blowing to me is that
else if
isn’t a keyword. There is onlyelse
andif
. Python had to introduce a third keyword becauseelse if
isn’t valid syntax. In other words,else if
comes for free in C-like languages due to the language grammar, and I had just assumed it was a special token.I was just wondering to myself why python would have a separate elif keyword. But of course it makes sense because of whitespace having meaning in python.
Thanks!
Quote from python documentation to back me up on this:
gave a heart just beacuse of P.S. :D
I'd never before considered that anyone would have thought of "else if" as a single keyword. I've learnt stuff today though about Python (because it would need to be
else: if
and that ain't gonna work) and went away to look up something else that niggled.PHP has "elseif" as an equivalent of "else if" and I'd never understood why they'd bother (except that PHP does a lot of crazy stuff). Turns out that it's for the "colon" syntax:
Yeah, I suppose I just lumped the idea of
else if
as another keyword when writing branches. It never occurred to me thatelse
andif
were separate, and playing in languages like Python only reinforced that idea.Granted, I'm sure it would have been more clear had Java not been my first language. I don't feel like you get to appreciate language syntax nowadays when languages are more tools than a form of art.
At any rate, interesting point about PHP! So, it has a mixed syntax (i.e. you can use
else if
with braces orelseif
with a colon)?Yeah, it's mostly so you can use it in templates (since it was a templating language to begin with really):
It's a big ugly, but it's better than this:
I can imagine that second syntax is a nightmare to debug. haha
My first reaction: of course it doesn't exist there's a space between else and if. But then again second reaction was but hey Ruby does have elsif and what's the actual reason for it. Anyway not impressed by else if part that much (i didn't actually care about those) but something similar in C# I was disappointed is switch case which works a bit different than in Java because I used to propagate steps but it's kind of bad practice so... I'm not sure about what gets me happy or mindblown except for null handling operators like elvis ?: or null coalescing ?? or conditional member acces ?. It's good to have posts like these just to make you think about stuff you don't care/notice otherwise. Thanks
I'm not sure that a space necessarily indicates separate keywords. It all depends on how the grammar is written. What's stopping a language grammar from containing
else if
as a terminal symbol?Nothing but my expectations are limited to that space. Even programming languages should think about UX. If you put else if as keyword with a space I wouldn't expect it to be 1 keyword but 2. But that's just my subjective view on that point.
How is this a problem? Code with lots of branches is a design smell; don't do that. Also, consider using the switch statement, which is getting more powerful with recent java releases. Or better, switch to Kotlin which has a nice when statement that is a bit more sane than switch or if else garbage.
I don’t think I ever said it was a problem. I was acknowledging the fact that the Java grammar doesn’t contain an else if terminal symbol. Of course, the content was geared toward beginners (i.e. my students), so I chose not to dig into compilers. Instead, I shared an example they could easily understand.
Naturally, the example is purely for the sake of argument—not something you should put in production code. Of course, you’re welcome to lecture me on which tools I should be using.
Same as in good old C. To me it was strange back then when I first saw elif in Python. :-)
😮😮 thanks for the post, I never thought about it like that either 👍👍
Glad you liked it! It makes me wonder what other kinds of language-related things I’ve been missing.
The tweet mentions 'JS', not Java as far as I can tell?
It does mention JS, but the syntax is the same in all c-like languages.