has use of "break" changed recenty?

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

Pedantic: break is a statement, not a function /Pedantic
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 , what time machine did you use to access this documentation? Your comment is dated Dec-2020 but the online documentation prior to 15b was removed over the summer. Is this documentation a local copy?
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.

Sign in to comment.

 Accepted Answer

Star Strider
Star Strider on 11 Oct 2016
Edited: Star Strider on 11 Oct 2016
It has, several versions ago. If you want to stop a script outside of a loop, use return instead of break. It does the same thing, and won’t throw an error.
To wit:
fprintf(1, 'Before ''return''\n')
return
fprintf(1, 'After ''return''\n')
EDIT Added demonstration code snippet.

3 Comments

Much obliged. Found the references to change to return, but was not sure when the change had been effected. Cheers
My pleasure.
If my Answer helped solve your problem, please Accept it.
Great tip, thanks!

Sign in to comment.

More Answers (0)

Asked:

on 11 Oct 2016

Commented:

Rik
on 13 Dec 2020

Community Treasure Hunt

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

Start Hunting!