Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Wash, Rinse, Repeat; Break, Return, Continue

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.




Published with MATLAB® 7.12


  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。