There is solution to "a break in a while loop cannot return a value" inspired by Python:
Python's while (and for) loop can have else: branch that is executed if loop exited with condition and not with break: https://docs.python.org/3.10/reference/compound_stmts.html#the-while-statement
This construction is unusual, rare language has else for loops (I don't remember one except Python). Therefore almost nobody uses it.
But it could naturally complement "break may return value from loop" feature (which Python has not). It could sound like:
"if while's loop body has break with value, then it has to have else block which results with value of same type".
There is solution to "a
break
in awhile
loop cannot return a value" inspired by Python:while
(andfor
) loop can haveelse:
branch that is executed if loop exited with condition and not with break: https://docs.python.org/3.10/reference/compound_stmts.html#the-while-statement This construction is unusual, rare language haselse
for loops (I don't remember one except Python). Therefore almost nobody uses it.break
may return value from loop" feature (which Python has not). It could sound like: "ifwhile
's loop body hasbreak
with value, then it has to haveelse
block which results with value of same type".