Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Break Function

Subject: Break Function

From: Konst

Date: 20 May, 2004 03:53:02

Message: 1 of 2

The break function only takes you out of a single for loop. Suppose one has
two for loops and one if with an else statement which contains a break.How
does one get out of both 'for' loops with one statement???

e.g.
xe=10;
for k=1:10
    for f=1:7
        xe=xe+k
        xe2=xe+f
        if xe<=20
            display('1on');
        else
            break
            break
        end
    end
end

Obviously this does not work.Any ideas before i do this:
xe=10;
for k=1:10
    for f=1:7
        xe=xe+k
        xe2=xe+f
        test=1;
        if xe<=20
            display('1on');
        else
            test=2;
            break
        end
    end
    if test=2
        break
    end
end

Thanking you in anticipation...

Subject: Break Function

From: Slappy

Date: 20 May, 2004 04:24:39

Message: 2 of 2

Unfortunately, Matlab offers no direct way to do this.

You could put the loops within their own function and use "return" from
within the nested iteration to exit both loops.

"konst" <konkonst@ee.duth.gr> wrote in message
news:c8gvh5$icu$1@nic.grnet.gr...
> The break function only takes you out of a single for loop. Suppose one
has
> two for loops and one if with an else statement which contains a break.How
> does one get out of both 'for' loops with one statement???
>
> e.g.
> xe=10;
> for k=1:10
> for f=1:7
> xe=xe+k
> xe2=xe+f
> if xe<=20
> display('1on');
> else
> break
> break
> end
> end
> end
>
> Obviously this does not work.Any ideas before i do this:
> xe=10;
> for k=1:10
> for f=1:7
> xe=xe+k
> xe2=xe+f
> test=1;
> if xe<=20
> display('1on');
> else
> test=2;
> break
> end
> end
> if test=2
> break
> end
> end
>
> Thanking you in anticipation...
>
>

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us