Borrar filtros
Borrar filtros

use of "break"

2 visualizaciones (últimos 30 días)
Mingze Yin
Mingze Yin el 28 de Feb. de 2022
Comentada: Voss el 1 de Mzo. de 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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by