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