how to exit from two nested for loop in matlab

loops, matlab

Solution

Here is a very simple answer leveraging the fact that testing numerous simple conditions is nearly free:

while (1)
    go = true;
    for x=1:20
        for y=1:30
            if go && condition
               go = false;

            end
        end
    end
end

This approach is very simple, easily generalized to any number of loops and avoids the abuse of error handling.

Problem

I have a `while` loop in which I have two `for` loops. I have a condition in the innermost `for` loop. Whenever that condition is satisfied I want to exit from both the two `for` loops and continue within the `while` loop: ``` while (1) for x=1:20 for y=1:30 if(condition) end end end end ``` Does Matlab have something like a labeled statement in Java, or is there another way to do this?

Original source

Related problems