has use of "break" changed recenty?
Show older comments
I have script which has functioned well under previous version of MatLab, recently updated the software to 2016a, and now get following error when running script.
Error: A BREAK may only be used within a FOR or WHILE loop, and then only within the same file as its corresponding FOR or WHILE statement.
Has the use of the break function changed?
4 Comments
Guillaume
on 11 Oct 2016
Pedantic: break is a statement, not a function /Pedantic
Rik
on 10 Dec 2020
You must be working with pretty old code. Time for some detective work:
Exiting a function with break does still work on R2015a, so this change must have been in either R2015b or R2016a.
But what does the documentation say? Let's hop in the time machine to check it out.
R2015a/R2018a/R2020b:
break is not defined outside a for or while loop. To exit a function, use return.
ML7.1 (R14SP3, released September 2005)/R2010a/R2011a:
break is not defined outside a for or while loop. Use return in this context instead.
That sounds more like it is discouraging, implying with the word 'instead' that this is a change. Let's look back a little further.
ML6.5 (R13, released July 2002):
break is not defined outside of a for or while loop. Use return in this context instead.
OK, so the word 'of' was removed in later releases. Not much of a change, if you ask me.
ML6.1 (R12.1, released June 2001):
If you use break outside of a for or while loop in a MATLAB script or function,
break terminates the script or function at that point.
If break is executed in an if, switch-case, or try-catch statement, it terminates
the statement at that point.
So finally we found it: this behavior was last properly documented when there was support for Windows 95.
Curiously, I would see more need in modern code, as something like this would be plausible:
while true
%%
x=rand;
if x>0.5
disp(x)
break % this doesn't work if you run the section
end
%%
end
Rik
on 13 Dec 2020
Yes, all of this is from local copies (only some of these required a VM). So unfortunately the time machine in question is metaphorical. The Wayback Machine will probably not have many of these.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!