{"id":284,"date":"2011-08-04T13:30:13","date_gmt":"2011-08-04T13:30:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/08\/04\/wash-rinse-repeat-break-return-continue\/"},"modified":"2011-07-15T13:32:15","modified_gmt":"2011-07-15T13:32:15","slug":"wash-rinse-repeat-break-return-continue","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/08\/04\/wash-rinse-repeat-break-return-continue\/","title":{"rendered":"Wash, Rinse, Repeat; Break, Return, Continue"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>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,\r\n         repeat\" to indicate this.  It's true for gathering requirements, fleshing out APIs, designing the architechture and test,\r\n         and, of course, creating documentation and demos and examples.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">break and continue<\/a><\/li>\r\n         <li><a href=\"#4\">return<\/a><\/li>\r\n         <li><a href=\"#8\">Have You Been Tripped Up by break, continue, return?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>And so it goes with life.  I will soon be out of the office for a few weeks.  So I will be taking a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/break.html\"><tt>break<\/tt><\/a>, next I will <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/return.html\"><tt>return<\/tt><\/a>, and finally I will <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/continue.html\"><tt>continue<\/tt><\/a> my work. So I thought I would talk about these three MATLAB commands today, especially since I've seen them misunderstood\r\n      from time to time.\r\n   <\/p>\r\n   <h3>break and continue<a name=\"2\"><\/a><\/h3>\r\n   <p>The functions <tt>break<\/tt> and <tt>continue<\/tt> are meant to be used in the context of loops, either <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/for.html\"><tt>for<\/tt><\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/while.html\"><tt>while<\/tt><\/a>.\r\n   <\/p>\r\n   <p>Here's a quick example of each behavior - very contrived.  The documentation has some others, focusing on reading files, should\r\n      that be of more interest to you.\r\n   <\/p>\r\n   <p>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!\r\n       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\r\n      10 yet, and once there are, I break out of the loop.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">prms = zeros(10,1);\r\ncount = 1;\r\ncurrent = 2;\r\n<span style=\"color: #0000FF\">while<\/span> true\r\n    <span style=\"color: #0000FF\">if<\/span> isprime(current)\r\n        prms(count) = current;\r\n        <span style=\"color: #0000FF\">if<\/span> count == 10\r\n            <span style=\"color: #0000FF\">break<\/span>;\r\n        <span style=\"color: #0000FF\">end<\/span>\r\n        count = count + 1;\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n    current = current + 1;\r\n<span style=\"color: #0000FF\">end<\/span>\r\nprms<\/pre><pre style=\"font-style:oblique\">prms =\r\n     2\r\n     3\r\n     5\r\n     7\r\n    11\r\n    13\r\n    17\r\n    19\r\n    23\r\n    29\r\n<\/pre><p>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.\r\n       Otherwise I continue in a similar manner as above.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">prms = zeros(10,1);\r\ncount = 1;\r\ncurrent = 1;\r\n<span style=\"color: #0000FF\">while<\/span> true\r\n    current = current + 1;\r\n    <span style=\"color: #0000FF\">if<\/span> ~isprime(current)\r\n        <span style=\"color: #0000FF\">continue<\/span>\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n    <span style=\"color: #228B22\">% current is prime<\/span>\r\n    prms(count) = current;\r\n    <span style=\"color: #0000FF\">if<\/span> count == 10\r\n       <span style=\"color: #0000FF\">break<\/span>;\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n    count = count + 1;\r\n <span style=\"color: #0000FF\">end<\/span>\r\nprms<\/pre><pre style=\"font-style:oblique\">prms =\r\n     2\r\n     3\r\n     5\r\n     7\r\n    11\r\n    13\r\n    17\r\n    19\r\n    23\r\n    29\r\n<\/pre><h3>return<a name=\"4\"><\/a><\/h3>\r\n   <p><tt>return<\/tt> returns from the currently executing function to the one from which it was called.  You can use this to return from a function\r\n      before running all the code, should sufficient conditions merit it.  Here's a function that returns early when enough prime\r\n      numbers are found, even if the limit to look up until would produce more.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">earlyReturn<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction prms = earlyReturnPrime(maxN,findN)\r\n% Find first findN primes up to the maximum value MaxN.\r\nprms = zeros(findN,1);\r\nprms(1) = 2;\r\ncount = 2;\r\nfor ind = 3:maxN\r\n    if isprime(ind)\r\n        prms(count) = ind;\r\n        count = count + 1;\r\n        if count &gt; findN\r\n            return\r\n        end\r\n    end\r\nend\r\n% Remove trailing zeros, if any.\r\nprms(count:end) = [];\r\n    \r\n    \r\n<\/pre><p>Let's run this.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">earlyReturn(7,4)\r\nearlyReturn(100,4)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     2\r\n     3\r\n     5\r\n     7\r\nans =\r\n     2\r\n     3\r\n     5\r\n     7\r\n<\/pre><p>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\r\n      number 7.\r\n   <\/p>\r\n   <p>I also elected to trim the output to a shorter length if not enough primes were found so there would not be trailing zeros\r\n      returned. In fact, there are 25 primes &lt;= 100.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">length(earlyReturn(100,41))\r\nlength(earlyReturn(100,25))\r\nlength(earlyReturn(100,17))<\/pre><pre style=\"font-style:oblique\">ans =\r\n    25\r\nans =\r\n    25\r\nans =\r\n    17\r\n<\/pre><h3>Have You Been Tripped Up by break, continue, return?<a name=\"8\"><\/a><\/h3>\r\n   <p>Have these functions every tripped you up?  Perhaps you can tell me how <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=284#respond\">here<\/a>.  In the meantime, I hope you get to take <b>break<\/b>, <b>return<\/b> refreshed, ande <b>continue<\/b>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5d953e2852eb463686c6b5e73aa8fd5e() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5d953e2852eb463686c6b5e73aa8fd5e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5d953e2852eb463686c6b5e73aa8fd5e';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5d953e2852eb463686c6b5e73aa8fd5e()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.12<br><\/p>\r\n<\/div>\r\n<!--\r\n5d953e2852eb463686c6b5e73aa8fd5e ##### SOURCE BEGIN #####\r\n%% Wash, Rinse, Repeat; Break, Return, Continue\r\n% So much of the work we do in development at MathWorks is iterative.  Over\r\n% the years, we've come to use the phrase \"wash, rinse, repeat\" to indicate\r\n% this.  It's true for gathering requirements, fleshing out APIs, designing\r\n% the architechture and test, and, of course, creating documentation and\r\n% demos and examples.\r\n%%\r\n% And so it goes with life.  I will soon be out of the office for a few\r\n% weeks.  So I will be taking a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/break.html\r\n% |break|>, next I will\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/return.html\r\n% |return|>, and finally I will\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/continue.html\r\n% |continue|> my work. So I thought I would talk about these three MATLAB\r\n% commands today, especially since I've seen them misunderstood from time\r\n% to time.\r\n%% break and continue\r\n% The functions |break| and |continue| are meant to be used in the context\r\n% of loops, either\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/for.html\r\n% |for|> or\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/while.html\r\n% |while|>.\r\n%\r\n% Here's a quick example of each behavior - very contrived.  The\r\n% documentation has some others, focusing on reading files, should that be\r\n% of more interest to you.\r\n%\r\n% First let me find the first 10 prime numbers.  Clearly there are better\r\n% ways to do this, so don't nitpick on the example please!  The algorithm\r\n% here is to march along the integers, looking for primes.  I check each\r\n% time I add a prime to see if there are 10 yet, and once there are, I\r\n% break out of the loop.\r\nprms = zeros(10,1);\r\ncount = 1;\r\ncurrent = 2;\r\nwhile true\r\n    if isprime(current)\r\n        prms(count) = current;\r\n        if count == 10\r\n            break;\r\n        end\r\n        count = count + 1;\r\n    end\r\n    current = current + 1;\r\nend\r\nprms\r\n%%\r\n% I could have done it this way instead.  Now I first check if the number\r\n% is not prime, and if not, skip the rest of the loop.  Otherwise I\r\n% continue in a similar manner as above.\r\nprms = zeros(10,1);\r\ncount = 1;\r\ncurrent = 1;\r\nwhile true\r\n    current = current + 1;\r\n    if ~isprime(current)\r\n        continue\r\n    end\r\n    % current is prime\r\n    prms(count) = current;\r\n    if count == 10\r\n       break;\r\n    end\r\n    count = count + 1;\r\n end\r\nprms\r\n%% return\r\n% |return| returns from the currently executing function to the one from\r\n% which it was called.  You can use this to return from a function before\r\n% running all the code, should sufficient conditions merit it.  Here's a\r\n% function that returns early when enough prime numbers are found, even if\r\n% the limit to look up until would produce more.\r\ntype earlyReturn\r\n%%\r\n% Let's run this.\r\nearlyReturn(7,4)\r\nearlyReturn(100,4)\r\n%%\r\n% The function returns early when I ask for the first 4 primes less than\r\n% 100, since it finds them by the time it checks the number 7.\r\n%%\r\n% I also elected to trim the output to a shorter length if not enough\r\n% primes were found so there would not be trailing zeros returned. In fact,\r\n% there are 25 primes <= 100.  \r\nlength(earlyReturn(100,41))\r\nlength(earlyReturn(100,25))\r\nlength(earlyReturn(100,17))\r\n%% Have You Been Tripped Up by break, continue, return?\r\n% Have these functions every tripped you up?  Perhaps you can tell me how\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=284#respond here>.  In the meantime,\r\n% I hope you get to take *break*, *return* refreshed, ande *continue*.\r\n\r\n\r\n##### SOURCE END ##### 5d953e2852eb463686c6b5e73aa8fd5e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      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,\r\n         repeat\" to indicate this.  It's true... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/08\/04\/wash-rinse-repeat-break-return-continue\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/284"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}