8 comments

[ 2.6 ms ] story [ 31.2 ms ] thread
Surprising. I didn't know this construct and I would naturally assume that the else part would execute if the loop never spun (because the list/etc was empty)
Yes, the else keyword is not intuitive. The best way to remember for/else is for/nobreak
Right! nobreak would make it much clearer.
It would involve adding a new keyword to the language though. I imagine there was some desire to avoid that.
Relatedly there's also a try/else, where the else block only runs if there wasn't an exception.

All these else variants come in handy from time to time.

(comment deleted)
The most common use case of for/else for me is:

for/len==0