필터 지우기
필터 지우기

How does 'break' work?

조회 수: 30 (최근 30일)
Sherwin
Sherwin 2016년 10월 27일
댓글: Sherwin 2016년 10월 27일
Hi, I need to know what happens after "break"? what does the loop skip to? I need it to skip to "for i = 1:50" and start from a new i. Please help me.
for i = 1:50
if a {i,5}(1) <= a{1,4}(1)
b{i,1} = a{1,1};
else
for j = 1:50
if a {i,5}(1) >= a{j,4}(1) && a{i,5}(1) <= a{j+1,4}(1)
b{i,1} = a{j+1,1};
break
end
end
end
end
  댓글 수: 2
Adam
Adam 2016년 10월 27일
doc break
is a useful start.
Sherwin
Sherwin 2016년 10월 27일
Thank you.

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

채택된 답변

James Tursa
James Tursa 2016년 10월 27일
편집: James Tursa 2016년 10월 27일
The "break" statement breaks out of the "innermost" loop that it is inside of. In your code above, it will break out of the for j=1:50 loop and start executing at the next line after the "j loop" end statement. This next statement happens to be the end statement in an else clause. Since there is no "i loop" code after this end statement other than the "i loop" end statement, this break will effectively cause the for i=1:50 loop to skip to the next i also.
  댓글 수: 3
James Tursa
James Tursa 2016년 10월 27일
Yes. Right after b{i,1} is assigned, it will break out of the "j loop". The next statement it hits is the "end" associated with the "else" block. So it exits this "else" block. The next statement it hits is the "end" associated with the "i loop". So this will effectively cause the code to skip to the next "i" iteration.
Sherwin
Sherwin 2016년 10월 27일
Thank you so much!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 10월 27일
"break" lets some of the Magic Smoke out of your loop. It is sort of like letting some of the air out of your tires -- while you are driving on the highway!
  댓글 수: 1
Sherwin
Sherwin 2016년 10월 27일
Beautiful metaphor! Thank you :)

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

카테고리

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