필터 지우기
필터 지우기

How to exist out of 2 for loops.

조회 수: 2 (최근 30일)
Max
Max 2015년 10월 24일
댓글: Walter Roberson 2015년 10월 24일
for i=1:(q+1)
......
for ...
if ...
break
end
end
end
Im having trouble breaking out of the first for loop. My code runs and finishes the second for loop. I want it to stop after the second for loop is finished running but instead it goes back to the first for loop and starts the whole thing over which obviously messes up the values I've produced in the second for loop. Basically I want the whole script to end after the second for loop is finished fully running.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 10월 24일
You could call quit, which would exit MATLAB entirely. If you were within a function, you could call return to leave the function.
Otherwise you cannot exit two nested loops. You can, however, set a flag that indicates you want to exit the outer loop and have the outer loop test it.
done_this_stuff = false;
for K = ...
for J = ...
if ...
done_this_stuff = true;
break
end
...
end
%now we are in just the outer loop
if done_this_stuff
break
end
...
end
  댓글 수: 2
Max
Max 2015년 10월 24일
I'm not really sure what you mean, I have sent you a direct message via your profile.
Walter Roberson
Walter Roberson 2015년 10월 24일
done_this_stuff = false;
for i=1:(q+1)
......
for ...
if ...
done_this_stuff = true;
break
end
end
if done_this_stuff
break
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by