Ćukasz Langa wrote: Alternatively, the only way I think we can improve on
the syntax above is something like this:
do: <code> if condition: break
One of the problems with a 'while' clause as suggested is that the comprehension version of it would complicate the equivalence rules between comprehensions and statements. Currently, a comprehension such as [... for a in x if c] corresponds to for a in x: if c: ... But if we take a comprehension with a 'while' clause, [... for a in x while c] and try to expand it the same way, we get for a in x: while c: ... which is not the same thing at all! To make it work, we need to expand it as for a in x: if not c: break But this only works if there is only one for-loop. If we have two nested loops, [... for a in x for b in y while c] the following expansion doesn't work, for a in x: for b in y: if not c: break because the break only exits the innermost loop. The proposed 'while' clause has a similar problem. In for a in x: for b in y while c: ... the 'while' would presumably break only the innermost loop. If we try to write it like this instead, for a in x while c: for b in y: ... we have a problem if the condition c involves both a and b. I'm not sure what the lesson is from all this. I think it's that any form of early-exit construct, including 'break' and the proposed 'while' clause, is a hack of limited usefulness without some way of scoping the amount of stuff to be broken out of. Comprehensions provide a natural way of specifying that scope, whereas the looping statements don't. So, I would probably support adding a 'while' clause to comprehensions, but not to the for-loop statement. -- Greg