6

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?

6

7 Answers 7

7

One ever so slightly more elegant way than Luis Mendo's. ;-)

while (1)
    for x=1:20
        for y=1:30
            quit = (condition);
            if quit
                break;
            end
        end
        if quit
            break;
        end
    end
end
Sign up to request clarification or add additional context in comments.

Comments

5

Only slightly more elegant than A.Donda's answer (avoids testing the condition twice):

while 1
    for x=1:20
        for y=1:30
            quit = 0;
            if (condition)
                quit = 1;
                break;
            end
        end
        if quit
            break;
        end
    end
end

6 Comments

While it does avoid testing the condition twice, it adds (quite a lot of) extra tests for quit...
@EitanT "Extra" compared to what? You either test for quit or for the condition (which takes longer), right?
Compared to, say, your solution that "flattens" the loop and uses ndgrid. There's no need to test quit in every loop iteration, you're already testing for the condition.
@EitanT You mean in every iteration of the outer lopp, right? Yes, you could save that. However, the bottleneck here would be testing within the inner loop (which is done 30 times more often), and that would not be avoided.
I am obviously talking about the outer loop :) I think that the numbers are arbitrary, and the flattened-loop solution is a safer bet in any case. But I'm just nitpicking.
|
4

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.

Comments

2

Another possibility, which avoids using two break statements: if you don't do anything in the outer for loop except calling the inner for loop, you can merge them:

[yy xx] = ndgrid(1:20,1:30);
while 1
    for n = 1:numel(x)
        x = xx(n);
        y = yy(n);
        if (condition)
            break      
        end
    end
end

Comments

2

A bit of abuse, a bit of overkill, but here it is: yet another way

while (1)
    try
        for x = 1 : 20
            for y = 1 : 30
                assert(~(condition), 'break')
            end
        end
    catch err
        if ~strcmp(err.message, 'break'), rethrow(err), end
    end
end

The nice thing about this approach is that it works with an arbitrary number of nested loops, and only evaluates condition once without having to store the result in a variable.

Inspired by tmpearce's comment.

3 Comments

+1 Good idea! Although as a general rule I try to avoid try-catch as much as possible; but that's just me...
Your second answer is still my favourite :-)
No, you're right. As I said, a bit of abuse. try/catch should be used for error management and nothing else. :) Thx.
1

I'm aware of only one quite inelegant way to do this: check the condition twice.

while (1)
    for x=1:20
        for y=1:30
            if (condition)
                break;
            end
        end
        if (condition)
            break;
        end
    end
end

1 Comment

@AmitKumar Note that the other answer by A.Donda avoids checking the condition twice
0

immediate exit for both for loops and continue with the remain of while loop.

while (1)
    go=1 %

    for x=1:20
        if go==0;break; %

        for y=1:30
            if(condition)
                go=0;break; %
            end
        end
    end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.