{"id":7073,"date":"2014-12-15T18:16:16","date_gmt":"2014-12-15T18:16:16","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=7073"},"modified":"2014-12-15T18:16:16","modified_gmt":"2014-12-15T18:16:16","slug":"computer-science-the-while-loop","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/computer-science-the-while-loop\/","title":{"rendered":"Computer science:  the while loop"},"content":{"rendered":"<h1>Tutoring computer science, a tenet you need to impart is the use of loops to accomplish repetitive tasks. \u00a0The tutor introduces the while loop.<\/h1>\n<p>Every programming language I know of has a &#8220;while&#8221; loop. \u00a0A while loop begins by checking a condition. \u00a0Finding the condition true, it executes one or more instuctions in the &#8220;loop body&#8221;. \u00a0Finding the condition false, it skips over the loop body, not to return.<\/p>\n<p>Here&#8217;s an example of a Perl while loop:<\/p>\n<p><span style=\"color:brown;font-family:monospace\">#!\/usr\/bin\/perl<br \/>\n$i=0;<br \/>\nwhile($ARGV[$i]){<br \/>\n$i++;<br \/>\n}<br \/>\nprint &#8220;There are $i command line arguments given.&#8221;;<br \/>\n<\/span><\/p>\n<p>The above program counts the inputs given with the function call on the command line.  For example:  suppose you save the program above as <span style=\"font-family:monospace\">argcount.txt<\/span>, then run it from the command line like so:<\/p>\n<p><span style=\"font-family:monospace\">perl argcount.txt Hello, how are you?<\/span><\/p>\n<p>You&#8217;ll receive the feedback<\/p>\n<p><span style=\"font-family:monospace\">There are 4 inputs given.<\/span><\/p>\n<p>Hence the &#8220;while loop&#8221; construct:  while a condition is met, the loop continues.  When the condition fails to be true, the program exits the loop.  This program checks for inputs.  While it keeps finding them, it repeats the &#8220;while&#8221; loop.  Failing to find any more, it exits the loop.<\/p>\n<p>Notice that, to check for a variable&#8217;s existence, Perl lets you use the minimal<br \/>\n<span style=\"font-family:monospace\">while($variable)<\/span>.  (I first mentioned this subtlety in my <a href=\"?p=5774\">Oct 2<\/a> post.)  Other programming languages can be more demanding, insisting on <span style=\"font-family:monospace\">while($variable!=null)<\/span> or something similar.  In my (somewhat limited) experience, Perl is the least demanding programming language.<\/p>\n<p>We&#8217;ll look more at Perl &#8211; as well as at a few of those &#8220;more demanding&#8221; programming languages &#8211; in future posts:)<\/p>\n<p>Source:<\/p>\n<p><a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">Robert&#8217;s Perl tutorial<\/a><\/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>Tutoring computer science, a tenet you need to impart is the use of loops to accomplish repetitive tasks. \u00a0The tutor introduces the while loop. Every programming language I know of has a &#8220;while&#8221; loop. \u00a0A while loop begins by checking &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/computer-science-the-while-loop\/\"> <span class=\"screen-reader-text\">Computer science:  the 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":[173,242],"class_list":["post-7073","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-perl-programming","tag-while-loop"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7073","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=7073"}],"version-history":[{"count":18,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7073\/revisions"}],"predecessor-version":[{"id":7091,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7073\/revisions\/7091"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=7073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=7073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=7073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}