`while-elif-else`, `for-elif-else`, `try-except-elif-else`

It is a bit simpler than that. It is the same as if-elif-else, except jump to elif-else is defined differently.

E.g.:

if cond:
    ...
# if not cond, proceed to other clauses
elif cond1:
elif cond2:
else:

versus:

while cond:
    ...
# if no break happened, proceed to other clauses
elif cond1:
elif cond2:
else:

After the comment, the same logic is applied to both cases.

Since you’re striving for consistency, how about adding support for break statements in if…elif…else control flow structures?

Edit: Actually, thinking about it some more, there would be more of a use case for if-local continue statements…

Re-using the else keyword in loop statements was arguably not a great decision. Some sources are in this case suggesting to translate in thought else → nobreak. I think it helps to cope with this confusing syntax to everybody learning the language.

But when the else is mentally replaced by nobreak, the combation nobreak + if = elif doesn’t feel natural making the mentioned tip less useful.

3 Likes

If this sort of mental teaching is wide spread, this change would surely be disruptive as it is based on a different mental model.

No one has taught me how to think about it and I came up with one myself. This was convenient, because it connected if-else and for-else so I needed to remember fewer independent concepts.

I waited for a year to post this so I could identify any possible red flags and such resistance came as a slight surprise to me.

I expected it to be more along the lines of:

  • “simply not worth it”
  • “no need to overcrowd the language unnecessarily”

and similar

but I see people are having real issues about this where I am still having hard time to see their POV.

I am not too fussed about this, I don’t think it is “needed”, but rather “MIGHT be nice to have” if people liked this.

Given that the interactions in this topic have not improved since a moderator last left a note and turned on slow mode, I’m now closing this.

If you think someone is responding inappropriately, flag it for a moderator, don’t start a back and forth argument in the thread, which just takes it off topic.

If you disagree with someone, or don’t think they understand your point of view after more than two posts, move on. Especially if that person is not someone who will be involved in implementing or maintaining your idea. You do not need to respond to every single point every single person brings up.

1 Like