Let's say I have I several nested for
loops in Ceylon. How do I break out of all the loops:
variable Integer? something = null;
for (i in 0:3) {
for (j in 0:3) {
for (k in 0:3) {
something = someFunction(i,j,k);
if (something exists) {
// break out of everything, we found it
}
}
}
}
One way to do it is to wrap the whole thing in a closure and then call it using return when you want to break out of everything:
Integer? doLoops() {
for (i in 0:3) {
for (j in 0:3) {
for (k in 0:3) {
Integer? something = someFunction(i,j,k);
if (something exists) {
return something;
}
}
}
}
return null;
}
Integer? something = doLoops();
Because Ceylon supports closures, you can also read and write other values outside the function in the scope where doLoops
is defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With