필터 지우기
필터 지우기

use of "break"

조회 수: 2 (최근 30일)
Mingze Yin
Mingze Yin 2022년 2월 28일
댓글: Voss 2022년 3월 1일
Hi everyone,
I have a few lines of code that look smth like this
exist = false;
for j = 1:1:J
if phi(m,j-1,n)*phi(m,j,n)<=0
x0 = SL+(j-2)*dS;
x1 = SL+(j-1)*dS;
y0 = phi(m,j-1,n);
y1 = phi(m,j,n);
boundary = LinearInterpolate(x0,x1,y0,y1);
exist = true;
end
end
First of all, im not confident that this is the right implementation. The purpose of these lines of code is to, by going through a for-loop, check the existence of this "boundary", and once such a boundary exists, set the variable "exist = true". I need this boolean variable because i need to use it for an if-statement immediately after (if exist == true ... else...).
My problem is that I do not think the codes above are executing the purpose as stated above? I kinda sense something is off but I can't quite tell which part isn't right... Would be really thankful if someone could point to me some ways to correct these codes. Thank you!

채택된 답변

Voss
Voss 2022년 2월 28일
If you are using the value of boundary immediately after that for loop, then you are using the value of boundary corresponding to the last "boundary" found within the loop.
It's not clear that this is the problem, but, if, for instance, you want the first boundary, you can break immediately after setting exist = true;, which will exit the loop and go to your if exist ... else ... statements.
If that's not the problem, then please describe what the code does vs what you want it to do.
  댓글 수: 7
Mingze Yin
Mingze Yin 2022년 3월 1일
Oh thank you so much for this, I actually have been oblivous to this function until now. I tried it and it's already helping me discover some overlooked problems in the program. Thanks again!
Voss
Voss 2022년 3월 1일
You're welcome! That's definitely a useful feature to know about (I use it all the time)

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

추가 답변 (0개)

카테고리

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