{"id":3750,"date":"2014-03-04T18:54:36","date_gmt":"2014-03-04T18:54:36","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=3750"},"modified":"2014-03-04T18:54:36","modified_gmt":"2014-03-04T18:54:36","slug":"computer-science-interpretation-of-the-perl-for-loop","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/computer-science-interpretation-of-the-perl-for-loop\/","title":{"rendered":"Computer science:  interpretation of the perl for loop"},"content":{"rendered":"<h1>Tutoring math, you are likely aware of its connection to computer science. \u00a0The math tutor continues the explanation of the perl for loop.<\/h1>\n<p>In my <a href=\"?p=3696\">previous post,<\/a> I began exploring the field of computer science with a look at a for loop. The loop was written in perl. Now I will explain, line by line, the meaning of the loop code:<\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">for ($i=0;$i&lt;3;$i++){<\/span><\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">print &#8220;The counter is now at $i.\\n&#8221;;<\/span><\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">}<\/span><\/p>\n<p>First off, the dollar sign in front of the i means that i is a variable. In perl, a variable has a dollar sign in front of it to show it&#8217;s a variable. Programmers often call a counter variable i, j, or k. So <span style=\"color: #440000; font-family: monospace;\">$i<\/span> means &#8220;the counter variable i&#8221;.<\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">$i&lt;3<\/span> means that as long as the value of i is less than three, the loop will continue running. When i equals 3, the program will exit the loop.<\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">$i++<\/span> means that every time the program executes the loop, it adds 1 to the counter variable (also known as <em>incrementing<\/em> the counter variable by 1).  Importantly, a for loop increments the counter variable at the end, rather than at the start.<\/p>\n<p>The brace <span style=\"color: #440000; font-family: monospace;\">{<\/span> means the program is entering the loop body, which contains the commands to be executed during each pass through the loop (also known as each <em>iteration<\/em> of the loop). In our case, there is only one instruction:<\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">print &#8220;The counter is now at $i.\\n&#8221;;<\/span><\/p>\n<p><span style=\"color: #440000; font-family: monospace;\">print<\/span> means display what&#8217;s in the quotation marks. The <span style=\"color: #440000; font-family: monospace;\">$i<\/span> is displayed as its value each time. Substituting the value of a variable in a quoted sentence is called <em>interpolation<\/em>. The <span style=\"color:#440000;font-family:monospace\">\\n<\/span> means &#8220;newline&#8221;.<\/p>\n<p>Now, the program reaches the closing brace <span style=\"color:#440000;font-family:monospace\">}<\/span>, which means the loop instructions are complete for this cycle.  It&#8217;s time to increment the counter and return to the beginning of the loop.  If <span style=\"color:#440000;font-family:monospace\">$i&lt;3<\/span>, the loop will execute again.<\/p>\n<p>Hence the output<\/p>\n<p>The counter is now at 0.<br \/>\nThe counter is now at 1.<br \/>\nThe counter is now at 2.<\/p>\n<p>Hope this clarifies the perl for loop.  In a coming post I&#8217;ll mention how to get (or find) perl on your own system, should you want to experiment with the code yourself:)<\/p>\n<p>Source:  <a href=\"http:\/\/perl.sioc.org\/win32perltut.html\">Robert Pepper&#8217;s Perl Tutorial<\/a>. Robert taught me almost everything I know about perl:)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutoring math, you are likely aware of its connection to computer science. \u00a0The math tutor continues the explanation of the perl for loop. In my previous post, I began exploring the field of computer science with a look at a &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/computer-science-interpretation-of-the-perl-for-loop\/\"> <span class=\"screen-reader-text\">Computer science:  interpretation of the perl for 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":[103,107,108,109,110,104,106],"class_list":["post-3750","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-for-loop","tag-increment","tag-interpolation","tag-iteration","tag-newline","tag-perl","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/3750","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=3750"}],"version-history":[{"count":35,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/3750\/revisions"}],"predecessor-version":[{"id":3785,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/3750\/revisions\/3785"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=3750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=3750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=3750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}