Open mrwright opened 10 years ago
I was thinking that they would act as they do in Python; see here. The summary:
it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
This is useful when searching for something with a loop; if you find it you break; if you fail, you run the else statement.
Okay, I do see why that's useful. Is there a more clever name for it than 'else'?
Yes. Clearly, it should be:
for (INIT-EXPRESSION; COND-EXPRESSION; LOOP-EXPRESSION)
STATEMENT
rof
STATEMENT
On IRC there was a suggestion of separate keywords for the two cases--perhaps broken
and completed
. (And it seems fine to allow either, none, or both, in any order, for a particular for
loop.)
I think this would also allow for
loops to be used as expressions (though any for
loop of non-unit type would need to specify both extra clauses).
Under what circumstances does the 'else' execute? I always found the concept of an 'else' on a loop to be unclear. Is it if we break, or if the condition becomes false (i.e. we don't break), or if the loop body doesn't execute at all?