{"id":29,"date":"2006-04-01T07:00:12","date_gmt":"2006-04-01T12:00:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=29"},"modified":"2018-01-08T16:16:12","modified_gmt":"2018-01-08T21:16:12","slug":"goto-coming-at-long-last-vectorized-no-less","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/04\/01\/goto-coming-at-long-last-vectorized-no-less\/","title":{"rendered":"Vectorized goto in MATLAB"},"content":{"rendered":"<p>Customers have long asked for a GOTO statement in MATLAB to allow them to use programming constructs familiar to them from other languages they already know.  I am pleased to announce that a proposal is making its way through the design process at MathWorks and I thought I'd use this opportunity to also get your feedback.  Search the <a>MATLAB newsgroup<\/a> to find many discussions on the topic of GOTO.\r\n<\/p>\r\n<p>\r\n<kbd>goto<\/kbd> statements can be invaluable in certain types of programs.  In fact, we do use it occasionally in the MATLAB C source code ourselves.  To quote a sage, \u201cThe problem with GOTOs is not the GOTO itself, but with the label.\u201d  So, we propose to introduce the <kbd>goto<\/kbd> statement into MATLAB, with the usual MATLAB unique twist.  Instead of labels identifying where to go, users can supply the destination(s) one of two ways:\r\n<ol>\r\n<li>an array of line numbers\r\n<\/li>\r\n<li>a cell array of strings, possibly with regular expressions, to denote the next lines of code containing the given expressions\r\n<\/li>\r\n<\/ol>\r\nUpon reaching a <kbd>goto<\/kbd>, MATLAB goes to each line \"simultaneously\" and proceeds from each location.\r\n<\/p>\r\n<p>The rules for <kbd>goto<\/kbd> are scoped to the file of interest,.  We still need to specify the rules.\r\n<\/p>\r\n<p>\r\nBehavior for line numbers:\r\n<ul>\r\n<li><kbd>Inf<\/kbd> : go directly to the end of the program without any further execution (think of it like <kbd>end<\/kbd>)<\/li>\r\n<li><kbd>NaN<\/kbd> : go to random places in the program and try to continue (Note: you might get errors because you might end up starting to evaluate something starting in the middle of a statement.)<\/li>\r\n<li>complex values: branch the <kbd>goto<\/kbd> so real parts of all data go to the real part of the number, and imaginary parts go to the imaginary portion of the number<\/li>\r\n<li><kbd>logical<\/kbd> : if <kbd>true<\/kbd> continue as if the <kbd>goto<\/kbd> was not there at all; if <kbd>false<\/kbd>, follow rules for <kbd>Inf<\/kbd><\/li>\r\n<li>non-integral numeric values : go to the line specified by the integer part of the number, and calculate the starting location in the line using the fractional portion of the number<li>\r\n<li>non-positive numbers : round up to 1, meaning go back to the beginning of the file<\/li>\r\n<\/ul>\r\n<\/p>\r\n<p>\r\nBehavior for cell arrays of strings:\r\n<ul>\r\n<li>found strings : go to the first line in the file with the string.  If a particular string is repeated go to the second\/third etc. instance and wrap back around to the first instance if you can't find more<\/li>\r\n<li>for strings that aren't present in the program : see behavior for <kbd>NaN<\/kbd><\/li>\r\n<li>no string matches found at all : see behavior for <kbd>NaN<\/kbd><\/li> \r\n<\/ul>\r\n<\/p>\r\n<p>Recent reactions to this proposal include this from Mike Karr:\r\n<blockquote>\r\nThis spec fills a much needed gap.\r\n<\/blockquote>\r\n<\/p>\r\n<p>\r\nFor more insight into other new features expected in MATLAB and other products, you might also be interested in Steve Eddins' <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=50\">blog post<\/a> this week as well.","protected":false},"excerpt":{"rendered":"<p>Customers have long asked for a GOTO statement in MATLAB to allow them to use programming constructs familiar to them from other languages they already know.  I am pleased to announce that a proposal... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/04\/01\/goto-coming-at-long-last-vectorized-no-less\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/29"}],"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=29"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":2618,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/29\/revisions\/2618"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}