This comes from a small thread in D.announce.
Sometimes you have loops inside switch cases, maybe like this,
and you want to exit the current switch case instead of just the
loop (or the loops):
void main() {
import std.stdio;
enum Foo { foo1, foo2 }
int[5] data;
Foo f;
switch (f) {
case Foo.foo1:
foreach (x; data)
if (x == 10)
goto END;
writeln("10 not found.");
data[0] = 10; // Put the missing 10.
END:
break;
default:
break;
}
}
In some of such situations sometimes I'd like a nicer way to exit
the case, maybe with a "break switch":
switch (f) {
case Foo.foo1:
foreach (x; data)
if (x == 10)
break switch;
writeln("10 not found.");
data[0] = 10; // Put the missing 10.
break;
A "break switch" is a bit like the D "break label", that allows
to break an outer loop:
loop1:
foreach (x; items1)
foreach (y; items2)
if (foo(x, y))
break loop1;
"break switch" in general is useful to express more clearly the
programmer intentions when there are loops inside switch cases.
Do you like? :-)
Bye,
bearophile