{"id":8961,"date":"2015-03-09T17:45:06","date_gmt":"2015-03-09T17:45:06","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=8961"},"modified":"2015-03-09T17:45:06","modified_gmt":"2015-03-09T17:45:06","slug":"perl-some-explanation-of-yesterdays-while-loop","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-some-explanation-of-yesterdays-while-loop\/","title":{"rendered":"Perl:  some explanation of yesterday&#8217;s while loop"},"content":{"rendered":"<h1>The tutor explains a few details about yesterday&#8217;s program.<\/h1>\n<p>The program, with the <span style=\"font-family:monospace\">while<\/span> loop and its support code added in <span style=\"font-family:monospace;color:orange\">orange<\/span>, is as follows:<br \/>\n<span style=\"font-family: monospace; color: brown;\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n%maritimecapitals=(&#8216;NB&#8217;,&#8217;Fredericton&#8217;,&#8217;NS&#8217;,&#8217;Halifax&#8217;,&#8217;PEI&#8217;,&#8217;Charlottetown&#8217;);<br \/>\n<span style=\"color: orange;\">$prov = &#8216;Y&#8217;;<br \/>\nwhile($prov ne &#8216;X&#8217; &amp;&amp; $prov ne &#8216;x&#8217;){<\/span><br \/>\nprint &#8220;Hello. Which Maritime capital would you like to know?\\n\\n&#8221;;<br \/>\nprint &#8220;Key in NS for Nova Scotia, NB for New Brunswick, or\\n\\n&#8221;;<br \/>\nprint &#8220;PEI for Prince Edward Island.\\n\\n&#8221;;<br \/>\n$prov=&lt;STDIN&gt;;<br \/>\nchomp $prov;<br \/>\n<span style=\"color: orange;\"><br \/>\nif($prov eq &#8216;X&#8217; || $prov eq &#8216;x&#8217;){<br \/>\nnext;<br \/>\n}<\/span><br \/>\nprint &#8220;The capital of $prov is $maritimecapitals{$prov}.\\n\\n&#8221;;<br \/>\n<span style=\"color: orange;\">}<br \/>\nprint &#8220;Thanks for playing :)\\n\\n&#8221;;<\/span><\/span><\/p>\n<p>The first orange line,<br \/>\n<span style=\"font-family:monospace;color:orange\">$prov=&#8217;Y&#8217;;<\/span><br \/>\nis needed because the <span style=\"font-family:monospace\">while<\/span> loop is about to check the value of <span style=\"font-family:monospace\">$prov<\/span>.  If the value of <span style=\"font-family:monospace\">$prov<\/span> is &#8220;not equal to&#8221; (which is what <span style=\"font-family:monospace\">ne<\/span> means) &#8216;X&#8217; or &#8216;x&#8217;, the program enters the while loop. To make sure that, on the first pass, <span style=\"font-family:monospace\">$prov ne &#8216;X&#8217; &#038;&#038; $prov ne &#8216;x&#8217;<\/span>, we set <span style=\"font-family:monospace\">$prov=&#8217;Y&#8217;<\/span>.  (<span style=\"font-family:monospace\">&#038;&#038;<\/span> means &#8220;and&#8221;).<\/p>\n<p>Next, we enter the <span style=\"font-family:monospace\">while<\/span> loop body.  The reddish-brown lines of code are likely self-explanatory to people who have read my other posts about Perl programming.  Then, we come to the orange<br \/>\n<span style=\"font-family:monospace;color:orange\">if($prov eq &#8216;X&#8217; || $prov eq &#8216;x&#8217;){<br \/>\nnext;<br \/>\n}<br \/>\n<\/span><\/p>\n<p>This <span style=\"font-family:monospace\">if<\/span> test checks if <span style=\"font-family:monospace\">$prov <\/span> is equal to (which is what <span style=\"font-family:monospace\">eq<\/span> means) &#8216;X&#8217; or &#8216;x&#8217;.  (<span style=\"font-family:monospace\">||<\/span> means &#8220;or&#8221;.)<\/p>\n<p>If indeed <span style=\"font-family:monospace\">$prov eq &#8216;X&#8217; || $prov eq &#8216;x&#8217;<\/span>, we enter the <span style=\"font-family:monospace\">if<\/span> body, where we reach the single instruction <span style=\"font-family:monospace\">next<\/span>.  The <span style=\"font-family:monospace\">next<\/span> instruction kicks the program back up to the top of the <span style=\"font-family:monospace\">while<\/span> loop, where <span style=\"font-family:monospace\">$prov<\/span> fails the <span style=\"font-family:monospace\">$prov ne &#8216;X&#8217; &#038;&#038; $prov ne &#8216;x&#8217;<\/span> condition.  The program bypasses the <span style=\"font-family:monospace\">while<\/span> loop to the last line of the program, and prints out<\/p>\n<p><span style=\"font-family:monospace\">Thanks for playing:)<\/span><\/p>\n<p>As long as the user doesn&#8217;t enter X or x, the program continues offering the game.  The <span style=\"font-family:monospace\">while<\/span> and <span style=\"font-family:monospace\">if<\/span> conditions check the value of <span style=\"font-family:monospace\">$prov<\/span> to know when the user desires to exit.<\/p>\n<p>I&#8217;ll be saying even more about the ideas mentioned here.  In the meantime,<br \/>\nHTH:)<\/p>\n<p>Sources:<\/p>\n<p><a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">Robert&#8217;s Perl tutorial<\/a><\/p>\n<p>McGrath, Mike.  <em>Perl in easy steps<\/em>.  Southam:  Computer Step, 2004.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor explains a few details about yesterday&#8217;s program. The program, with the while loop and its support code added in orange, is as follows: #!\/usr\/bin\/perl %maritimecapitals=(&#8216;NB&#8217;,&#8217;Fredericton&#8217;,&#8217;NS&#8217;,&#8217;Halifax&#8217;,&#8217;PEI&#8217;,&#8217;Charlottetown&#8217;); $prov = &#8216;Y&#8217;; while($prov ne &#8216;X&#8217; &amp;&amp; $prov ne &#8216;x&#8217;){ print &#8220;Hello. Which &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-some-explanation-of-yesterdays-while-loop\/\"> <span class=\"screen-reader-text\">Perl:  some explanation of yesterday&#8217;s while loop<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[579,583,578,580,582,173,581,242],"class_list":["post-8961","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-perl-and","tag-perl-and-symbol","tag-perl-if-statement","tag-perl-or","tag-perl-or-symbol","tag-perl-programming","tag-perl-while-loop","tag-while-loop"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=8961"}],"version-history":[{"count":20,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8961\/revisions"}],"predecessor-version":[{"id":8982,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8961\/revisions\/8982"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=8961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=8961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=8961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}