Filter löschen
Filter löschen

use of "break"

2 Ansichten (letzte 30 Tage)
Mingze Yin
Mingze Yin am 28 Feb. 2022
Kommentiert: Voss am 1 Mär. 2022
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!

Akzeptierte Antwort

Voss
Voss am 28 Feb. 2022
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 Kommentare
Mingze Yin
Mingze Yin am 1 Mär. 2022
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 am 1 Mär. 2022
You're welcome! That's definitely a useful feature to know about (I use it all the time)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by