{"id":7472,"date":"2024-09-13T10:12:24","date_gmt":"2024-09-13T08:12:24","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7472"},"modified":"2024-11-27T14:55:49","modified_gmt":"2024-11-27T13:55:49","slug":"9-3-loop-statements","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7472","title":{"rendered":"9.3 Loop statements"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7472&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7472&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">A loop statement specifies that one or more statements are executed a number of times and as mentioned, <code>cycle<\/code> and <code>for:to:repeat<\/code> are examples of loop statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining for:to:repeat<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>for:to:repeat<\/code> is defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   for(first: <strong>var<\/strong> integer):to(last: <strong>var<\/strong> integer):repeat{repeat:&lt; object}:\n      inx: <strong>var<\/strong> integer\n      inx := first\n      doIt: <strong>do<\/strong>\n         if (inx &lt;= last) :then\n            repeat\n            inx := inx + 1\n            restart(doIt)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>for:to:repeat<\/code> has the following structure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The name of the control method is <code>for:to:repeat<\/code>.<\/li>\n\n\n\n<li>It has three parameters: <code>first<\/code>, <code>last<\/code> and <code>repeat<\/code>.<\/li>\n\n\n\n<li>It has a local integer variable <code>inx<\/code>.<\/li>\n\n\n\n<li>It has a local <code>do<\/code>-object, which is executed as a statement &#8211; see section <script>mkRef(\"Statements\")<\/script>. <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">As explained in section <script>mkRef(\"Statements\")<\/script>, a <code>for:to:repeat<\/code> iterates over a range of integers and executes a set of items for each iteration:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The parameters <code>first<\/code> and <code>last<\/code> defines the range, where the first value is <code>first<\/code> and the last value is <code>last<\/code>.<\/li>\n\n\n\n<li>The variable <code>inx<\/code> has the value of the current element in the range. This means that <code>inx<\/code> goes through the values&nbsp;<code>start<\/code>,&nbsp;<code>start + 1<\/code>,&nbsp;<code>start + 1<\/code>, \u2026 ,&nbsp;<code>last<\/code>.<\/li>\n\n\n\n<li>For each value in the range, the virtual method parameter <code>repeat<\/code> is executed.<\/li>\n\n\n\n<li>If <code>start &gt; last<\/code>, then <code>repeat<\/code> is never executed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a simple example of using a <code>for:to:repeat<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A,B: <strong>obj<\/strong> Array(10,Integer)\nfor (1):to(10):repeat\n    A.put(inx * inx):at&#91;inx]\nfor (1):to(10):repeat\n    B.put(A.get&#91;inx] * A.get&#91;inx])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Defining while:repeat<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Another common control structure is the while-loop, which repeats executing a list of statements as long as a given condition is true. The following example shows a program fragment that computes 2<sup>4<\/sup>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a,i: var integer\na := 2\ni := 1\nwhile (i &lt; 5):repeat\n   a := a * 2\n   i := i + 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>while:repeat<\/code>-loop executes <code>a := a * 2; i := i + 1<\/code> as long as <code>i &lt; 5<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>while:repeat<\/code> is defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(cond:&lt; Condition):repeat{doPart:&lt; Object}:\n   loop: <strong>do<\/strong>\n      if (cond):then\n         doPart\n         restart(loop)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first parameter <code>cond<\/code>, is a virtual method qualified by the supermethod <code>Condition<\/code>. <code>Condition<\/code> returns a boolean value and it may be bound to boolean expressions like <code>i &lt; 5<\/code> as in the above example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second parameter <code>doPart<\/code>, may be bound to a list of items that will be executed as long as <code>cond<\/code> evaluates to true.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note the difference of the first parameter of <code>if:then<\/code> (and <code>if:then:else<\/code>) and <code>while:repeat<\/code>. For <code>if:then<\/code>, the first parameter is a boolean value since it is only evaluated at the point of invocation of <code>if:then<\/code>.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>cond<\/code> of <code>while:repeat<\/code> is, however, a virtual method since it must be evaluated for each iteration in the execution of the <code>while<\/code>-loop.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=7472\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>A loop statement specifies that one or more statements are executed a number of times and as mentioned, cycle and for:to:repeat are examples of loop statements. Defining for:to:repeat The for:to:repeat is defined as follows: A for:to:repeat has the following structure: As explained in section , a for:to:repeat iterates over a range of integers and executes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3671,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7472","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7472"}],"version-history":[{"count":10,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7472\/revisions"}],"predecessor-version":[{"id":9956,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7472\/revisions\/9956"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3671"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}