MATLAB Answers

How do I break out of nested loops using the BREAK command in MATLAB 7.7 (R2008b)?

829 views (last 30 days)
I am trying to break out of nested FOR loops using BREAK, but the control returns to the loop immediately above.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This functionality is not availble when using the function BREAK. BREAK will only break out of the loop in which it was called.
As a workaround, you can use a flag variable along with BREAK to break out of nested loops.
flag=0;
for i=1:10
for j=1:5
flag=1;
break
end
if(flag==1)
break
end
end
  2 Comments

Sign in to comment.

More Answers (2)

Mathieu Boutin
Mathieu Boutin on 26 Oct 2017
Edited: Mathieu Boutin on 26 Oct 2017
for i=1:10
for j=1:5
i=10; %Set i to the last value of the outer loop variable.
break
end
end
  4 Comments

Sign in to comment.


Muhammad nasiri
Muhammad nasiri on 23 Sep 2017
to skip the current iteration and move to next one, use continue commond for getting out of loop, use break
  3 Comments

Sign in to comment.

Products


Release

R2008b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!