So much of the work we do in development at MathWorks is iterative. Over the years, we've come to use the phrase "wash, rinse, repeat" to indicate this. It's true for gathering requirements, fleshing out APIs, designing the architechture and test, and, of course, creating documentation and demos and examples.
Contents
And so it goes with life. I will soon be out of the office for a few weeks. So I will be taking a break, next I will return, and finally I will continue my work. So I thought I would talk about these three MATLAB commands today, especially since I've seen them misunderstood from time to time.
break and continue
The functions break and continue are meant to be used in the context of loops, either for or while.
Here's a quick example of each behavior - very contrived. The documentation has some others, focusing on reading files, should that be of more interest to you.
First let me find the first 10 prime numbers. Clearly there are better ways to do this, so don't nitpick on the example please! The algorithm here is to march along the integers, looking for primes. I check each time I add a prime to see if there are 10 yet, and once there are, I break out of the loop.
prms = zeros(10,1); count = 1; current = 2; while true if isprime(current) prms(count) = current; if count == 10 break; end count = count + 1; end current = current + 1; end prms
prms =
2
3
5
7
11
13
17
19
23
29
I could have done it this way instead. Now I first check if the number is not prime, and if not, skip the rest of the loop. Otherwise I continue in a similar manner as above.
prms = zeros(10,1); count = 1; current = 1; while true current = current + 1; if ~isprime(current) continue end % current is prime prms(count) = current; if count == 10 break; end count = count + 1; end prms
prms =
2
3
5
7
11
13
17
19
23
29
return
return returns from the currently executing function to the one from which it was called. You can use this to return from a function before running all the code, should sufficient conditions merit it. Here's a function that returns early when enough prime numbers are found, even if the limit to look up until would produce more.
type earlyReturn
function prms = earlyReturnPrime(maxN,findN)
% Find first findN primes up to the maximum value MaxN.
prms = zeros(findN,1);
prms(1) = 2;
count = 2;
for ind = 3:maxN
if isprime(ind)
prms(count) = ind;
count = count + 1;
if count > findN
return
end
end
end
% Remove trailing zeros, if any.
prms(count:end) = [];
Let's run this.
earlyReturn(7,4) earlyReturn(100,4)
ans =
2
3
5
7
ans =
2
3
5
7
The function returns early when I ask for the first 4 primes less than 100, since it finds them by the time it checks the number 7.
I also elected to trim the output to a shorter length if not enough primes were found so there would not be trailing zeros returned. In fact, there are 25 primes <= 100.
length(earlyReturn(100,41)) length(earlyReturn(100,25)) length(earlyReturn(100,17))
ans =
25
ans =
25
ans =
17
Have You Been Tripped Up by break, continue, return?
Have these functions every tripped you up? Perhaps you can tell me how here. In the meantime, I hope you get to take break, return refreshed, ande continue.
Get
the MATLAB code
Published with MATLAB® 7.12



I typically use BREAK and RETURN to simplify error-handling logic. Without them, you can end up with deeply nested IF-THEN statements:
if condition1 abc; if condition2 efg; if condition3 % actual work end end endThe problem with the above code is that it is difficult to add a new condition. It is also easy to get confused with which END goes with which FOR. Here’s the same logic with RETURN:
BREAK is similarly useful if there are multiple complicated rules for ending a loop.
The main problem I’ve encountered with RETURN is ensuring that the function state is cleaned up properly (e.g., closing files) and that the outputs are correctly initialized. The onCleanup function can help with the former issue.
Not exactly, but recently this similar pattern emerged:
It looked very odd with the “quit” inside the try branch…
After pondering this for several years, my current thinking is that you end up with more robust code if you write it without either the return or break functions. This requires different approaches.
Gautam certainly illustrates a valid approach that avoids the complex nested if statements. However, I think that a switch-case sequence would be even better.
Talking about control structures, I miss the following ones in MATLAB:
or
I know it’s possible to use a while-loop and some helper variables instead, but that doesn’t look as nice as the above ones.
There are many cases in which you want to run through a loop at least once (under any circumstances) and maybe more often (depending on condition).