필터 지우기
필터 지우기

How to exit from loop function ?

조회 수: 4 (최근 30일)
Yamina chbak
Yamina chbak 2021년 5월 1일
댓글: Yamina chbak 2021년 5월 1일
Hi, I need to exit from a loop,should i use break ?
here is an example :
for j=1:10
if (condition 1)
a=5; % example
% continue step
elseif (condition 2)
a=2;
% continue
elseif (condition 3)
a=0;
% !!! reject step
break; % !!
end
What I do ?
Thanks.
  댓글 수: 6
Image Analyst
Image Analyst 2021년 5월 1일
@A Achbak, then why have you not accepted @Jan's answer below yet? It will give him "reputation points" for helping you. I'll at least Vote for his answer so he'll at least get some from me.
Yamina chbak
Yamina chbak 2021년 5월 1일
@Image Analyst Now I am accepted @Jan's answer. Sorry for being late : press Accepted answer.

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

채택된 답변

Jan
Jan 2021년 5월 1일
Simply try it:
a = 0;
for j = 1:10
fprintf('j=%d a=%d\n', j, a);
if j < 3
a = 5;
elseif j < 6
a = 2;
elseif j == 8
a = 0;
fprintf('Breaking the loop...\n');
break;
end
fprintf(' >> next iteration\n');
end
fprintf('Loop finished. a=%d\n', a);

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by