[–] bzb3 6y ago ↗ 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) [–] neverartful 6y ago ↗ This was my guess too. [–] amitutk 6y ago ↗ Yes, the else keyword is not intuitive. The best way to remember for/else is for/nobreak [–] neverartful 6y ago ↗ Right! nobreak would make it much clearer. [–] Dagger2 6y ago ↗ It would involve adding a new keyword to the language though. I imagine there was some desire to avoid that.
[–] amitutk 6y ago ↗ Yes, the else keyword is not intuitive. The best way to remember for/else is for/nobreak [–] neverartful 6y ago ↗ Right! nobreak would make it much clearer. [–] Dagger2 6y ago ↗ It would involve adding a new keyword to the language though. I imagine there was some desire to avoid that.
[–] neverartful 6y ago ↗ Right! nobreak would make it much clearer. [–] Dagger2 6y ago ↗ It would involve adding a new keyword to the language though. I imagine there was some desire to avoid that.
[–] Dagger2 6y ago ↗ It would involve adding a new keyword to the language though. I imagine there was some desire to avoid that.
[–] Doxin 6y ago ↗ 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.
8 comments
[ 2.6 ms ] story [ 31.2 ms ] threadAll these else variants come in handy from time to time.
for/len==0