Problem with code originally written for MATLAB R2006a -- problems with break statement

1 Ansicht (letzte 30 Tage)
Hi all,
I'm currently running a program with code that was originally written for MATLAB R2006a or R2007b for a research project, but I only have access to R2019b (through university computer access). There is an issue that seems to be due to a change in syntax from past versions, and I'm wondering if anyone has any insight to this issue.
The line of code that I am running into problems is:
if A==0;break;end
This returns an error statement of: '"BREAK can only be used in a FOR or WHILE loop"
I know there isn't a lot of detail in my problem, but I am relatively new to Matlab. Is there a way that this can be fixed? Any insight is greatly appreciated!
  2 Kommentare
the cyclist
the cyclist am 4 Mär. 2021
Bearbeitet: the cyclist am 5 Mär. 2021
This is difficult to diagnose without any more context, for a couple reasons, but mainly because it is not possible for us to understand the original programmer's intended behavior, from just this code snippet. Are you able to provide more of the surrounding code?
As an aside ... it could be my faulty memory, but I don't recall break ever being used to do anything other than escape from for and while loops. The way it appears here, inside an if-block, really looks like it might have been used in that way (meaning that this if-block was nested inside a loop).
Cris LaPierre
Cris LaPierre am 4 Mär. 2021
Bearbeitet: Cris LaPierre am 4 Mär. 2021
Is it possible this if statement was part of a for/while loop that is either commented out or that has been edited away? That is a valid use of break. See this example taken from the break documentation page.
limit = 0.8;
s = 0;
while 1
tmp = rand;
if tmp > limit
break
end
s = s + tmp;
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Mär. 2021
Long ago, no error message was generated for a break outside of any loop. I never used that behavior myself... I think I encountered it exactly once myself (in a situation where return was appropriate instead in that one case.)
I have read about it a very small number of times, and my memory is telling me that in the very small number of cases I saw, that the break could be commented out, as the logic was broken in the routines anyhow and failing to change flow of control did not make a substantial difference. Things like reading from the wrong directory, code that never worked properly, so commenting out the break just removed the syntax distraction to allow people to run the code and experience for themselves that the routine was rot.
  1 Kommentar
Justen Saini
Justen Saini am 5 Mär. 2021
Thank you very much! Turns out that I was able to remove the break from the code and it did not have any major consequences. I think it may only be a temporary fix and that there more adjustments that have to be made, but your advice was very helpful.
I appreciate your insight!

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

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by