{"id":963,"date":"2015-09-01T08:00:55","date_gmt":"2015-09-01T12:00:55","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=963"},"modified":"2024-03-01T17:27:58","modified_gmt":"2024-03-01T22:27:58","slug":"swift-repeat-keyword","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2015\/09\/01\/swift-repeat-keyword\/","title":{"rendered":"Swift &#8211; Repeat Keyword"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201509\/Repeat.png\" alt=\"Repeat\" title=\"Repeat\" style=\"float:left;margin:.5em;\" \/>In the Swift Programming Language &#8211; like all programming languages &#8211; we are given a lot of ways to control the flow of the program.  Back when Swift was first introduced, I <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2014\/06\/25\/swift-control-structures\/\">wrote a post<\/a> to talk about the ways that Swift offered to control program flow.<\/p>\n<p>One of the ways that I covered in that post was the <em>while<\/em> keyword.  Using a <em>while<\/em> statement will cause the program to evaluate a condition first before it would ever execute the block.  Take this code for example:<\/p>\n<pre>\r\nvar counter:Int = 0\r\n\r\nwhile counter &lt; 10 {\r\n    print(\"counter is at \\(counter)\")\r\n    counter++\r\n}\r\n<\/pre>\n<p>This results in the output of:<\/p>\n<pre>\r\ncounter is at 0\r\ncounter is at 1\r\ncounter is at 2\r\ncounter is at 3\r\ncounter is at 4\r\ncounter is at 5\r\ncounter is at 6\r\ncounter is at 7\r\ncounter is at 8\r\ncounter is at 9\r\n<\/pre>\n<p>However, if you ran the following code:<\/p>\n<pre>\r\nvar counter:Int = 0\r\n\r\nwhile counter &gt; 0 {\r\n    print(\"counter is at \\(counter)\")\r\n    counter++\r\n}\r\n<\/pre>\n<p>There is no output.  Since counter is 0 and the <em>while<\/em> block only executes for positive values, nothing happens.<\/p>\n<p>Let&#8217;s see how <em>repeat<\/em> is different.  We&#8217;ll try the first example again.<\/p>\n<pre>\r\nvar counter:Int = 0\r\n\r\nrepeat {\r\n    print(\"counter is at \\(counter)\")\r\n    counter++\r\n} while counter &lt; 10\r\n<\/pre>\n<p>This results in the same output of<\/p>\n<pre>\r\ncounter is at 0\r\ncounter is at 1\r\ncounter is at 2\r\ncounter is at 3\r\ncounter is at 4\r\ncounter is at 5\r\ncounter is at 6\r\ncounter is at 7\r\ncounter is at 8\r\ncounter is at 9\r\n<\/pre>\n<p>But, the second example of <\/p>\n<pre>\r\nvar counter:Int = 0\r\n\r\nrepeat {\r\n    print(\"counter is at \\(counter)\")\r\n    counter++\r\n} while counter &gt; 0\r\n<\/pre>\n<p>Results in the code running for ever and ever and ever.  It only asked if the variable was greater than 0 after the first pass.  Since by the time the code had exited the <em>repeat<\/em> block the value of counter was 1 (and therefore positive), we just kept going (I stopped it when the last few entries looked like this):<\/p>\n<pre>\r\ncounter is at 10789\r\ncounter is at 10790\r\ncounter is at 10791\r\ncounter is at 10792\r\ncounter is at 10793\r\ncounter is at 10794\r\ncounter is at 10795\r\ncounter is at 10796\r\ncounter is at 10797\r\ncounter is at 10798\r\ncounter is at 10799\r\n<\/pre>\n<p>You might be saying to yourself, but Pete.. this sounds exactly like a <em>do-while<\/em> loop vs a <em>while<\/em> loop. Well, you&#8217;d be 100% correct.  Even according to the Swift documentation, &#8220;The <em>repeat-while<\/em> loop in Swift is analogous to a <em>do-while<\/em> loop in other languages&#8221;.  C&#8217;est la vie!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the Swift Programming Language &#8211; like all programming languages &#8211; we are given a lot of ways to control the flow of the program. Back when Swift was first introduced, I wrote a post &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[131],"class_list":["post-963","post","type-post","status-publish","format-standard","hentry","category-swift","tag-swift"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}