13 ms·
There are languages with while cond1 stmts1 elif cond2 stmts2 elif cond3 stmts3 syntax which is essentially a few if/else blo
by Joker_vD 4y ago
There are languages with
while cond1
stmts1
elif cond2
stmts2
elif cond3
stmts3
syntax which is essentially a few if/else blocks pulled up to the enclosing loop level. Makes writing e.g.
while a > b
a := a - b
elif b > a
b := b - a
somewhat more concise. And Rust's "while let" loop easily could have allowed several clauses instead of single one.
And yes, you can express all of that with either "while true: ..." with early breaks/returns or with "while <OR'ing lots of Boolean flags>: ..." with post-processing those flags to determines why the loop has ended... but why not have explicitly named, limited continuations? IMO it would simplify dealing with "there maybe many reasons why the loop ended" kinds of iterations. Putting every loop into its own (anonymous) function that returns an ad-hoc sum type doesn't sound like a much better solution to me, to be fair.