I may have been exposed to exactly the wrong languages, but though many have loops and break statements, none of the languages I am familiar with have higher-order break statements¹. While a regular break statement terminates the innermost loop inside which it is executed, such a break(n)
statement would terminate the n
innermost loops.
For example using such a statement, I could write in Python:
for i in range(100):
for j in range(100):
if foo(i,j):
break(2)
As Python lacks such a statement, however, I have to do something like the following:
broken = False
for i in range(100):
for j in range(100):
if foo(i,j):
broken = True
break
if broken:
break
(I am aware that there are other ways to do this, such as raising exceptions. This serves just as an example.)
In another example, this is one of the prominent reasons to use a goto
statement in C/C++. While goto
allows for a close equivalent of a higher-order break, it introduces a new syntactical element (unless one already uses goto
s). Not to forget that goto
is frowned upon by many – be it justified or not.
While I see that using a higher-order break statement is not something most people use on a daily basis, this also applies to a lot of other language features. Also, a higher-order break statement does not seem difficult to implement to me and would be rather intuitive to use.
Is there a good reason why programming languages would have a break statement but not a higher-order break statement? I am thinking of things like inherent conceptual problems, broken paradigms or error vulnerability.
¹ Though, when researching for this question, I learnt that PHP has them.
break
in the first place are both ill-advised? (The latter more debatably) Languages have enough challenges without solving problems that people shouldn't be doing anyways.goto
, others havecall/cc
, orthrow
(and C haslongjmp
) ....