{"id":1431,"date":"2012-09-10T08:24:00","date_gmt":"2012-09-10T15:24:00","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1431"},"modified":"2012-09-10T08:24:00","modified_gmt":"2012-09-10T15:24:00","slug":"do-while","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1431","title":{"rendered":"do } { while"},"content":{"rendered":"<p>I&#8217;ve been coding in coding in various languages since around 1983 and this is the first time I&#8217;ve had an occasion to use a do } { while loop.<\/p>\n<p>Here\u2019s the scenario. I have four different kinds of pictures: locomotives, boxcars, tankers, and cabooses. I have eleven versions of each, one for each of eleven colors. I want to display two items on the screen at the same time and ask the child to identify the color. Now if a red boxcar and a red caboose show up on the screen at the same time, both are correct so the child can\u2019t choose the red one. So what I want to do is check to see if the colors are the same and then pick a different object if they are.<\/p>\n<p>I\u2019ve already picked my first object from the list and this is the code I use to pick the second. I always need to pick a second item, so the do { } while construction is perfect. It runs through the code and after the first pass evaluates the conditional. In this case, it checks to see if the color of the first object (leftItem) is the same as the color of the second object (rightItem). If they are equal, it does another iteration and picks another object. I have eleven colors so the loop repeats about 9% of the time, so it doesn\u2019t have any impact on execution. If you only had two or three colors, you\u2019d probably want to use a different method.<\/p>\n<p>Note: The code is Objective C and I changed it a bit from the original to make the loop portion clearer.<\/p>\n<p><pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\n&nbsp;&nbsp;NSInteger randomWord2;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Get another random number between 0 and n-1 \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/and add it to the original number plus 1\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int randomNumber2 = (arc4random() % numItems);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ If randomNumber2 is not zero then the two words will be different\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (randomNumber2&nbsp;&nbsp; == 0) randomNumber2 = self.scoreKeeper.currentScreen + 1;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;randomItem2 = (randomNumber2 + self.scoreKeeper.currentScreen) % numItems;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.rightItem = [self.wordList getWord:randomItem2];\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ If the two objects have the same color, look for another rightItem\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} while ( [self.rightItem.color isEqual:self.leftItem.color] );\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been coding in coding in various languages since around 1983 and this is the first time I&#8217;ve had an occasion to use a do } { while loop. Here\u2019s the scenario. I have four different kinds of pictures: locomotives, boxcars, tankers, and cabooses. I have eleven versions of each, one for each of eleven &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1431\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">do } { while<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1431","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1431"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1431\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}