{"id":1849,"date":"2023-11-30T17:26:55","date_gmt":"2023-11-30T16:26:55","guid":{"rendered":"http:\/\/oopm.org\/?page_id=1849"},"modified":"2024-12-20T14:27:14","modified_gmt":"2024-12-20T13:27:14","slug":"3-4-using-array-and-for-loop","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1849","title":{"rendered":"4.2 Array and for-loop"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1849&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=wpv2pages1849&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 customer may have more than one account. In this section, we extend class <code>Customer<\/code> to keep track of the accounts of a given customer.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">We may use class <code>Set<\/code> for this purpose, but we use this example to introduce <code>Array<\/code>, which is another common collection object. An <code>Array<\/code>-object is an indexed sequence of references to objects where each reference in the sequence may be denoted by an integer index.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"163\" height=\"162\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/image.png\" alt=\"\" class=\"wp-image-10732\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/image.png 163w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/image-150x150.png 150w\" sizes=\"(max-width: 163px) 100vw, 163px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left has-tiny-font-size wp-block-paragraph\">       Accounts Array<\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Array<\/code> is defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Array(range: <strong>var<\/strong> integer, <strong>class<\/strong> ElmType:&lt; Object):\n   put(R: <strong>ref<\/strong> ElmType):at&#91;index: <strong>var<\/strong> integer]:\n      ...\n   get(index: <strong>var<\/strong> integer) -&gt; R: <strong>ref<\/strong> ElmType:\n      ...<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first parameter, <code>range<\/code> of <code>Array<\/code> is the number of elements in the <code>Array<\/code>.<\/li>\n\n\n\n<li>The second paremeter, <code>ElmType<\/code>, is the type of the elements in the <code>Array<\/code> &#8211; similar to <code>ElmType<\/code> for class <code>Set<\/code>.<\/li>\n\n\n\n<li>It has a method <code>put:at<\/code> with two parameters <code>R<\/code> and <code>index<\/code>. This method stores the reference at the position given by the value of <code>index<\/code>.<\/li>\n\n\n\n<li>It has a method <code>get<\/code>, which returns the reference stored at the position given by the parameter <code>index<\/code>.<\/li>\n\n\n\n<li>As can be seen, <code>put:at<\/code> and <code>get<\/code> use another syntax for defining parameters than the one we have seen in previous examples using standard brackets and comma to separate the parameters. This syntax is explained in section <script>mkRef(\"Using fat comma for specifying parameters\") <\/script>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following example shows how we may use an <code>Array<\/code>-object, <code>accounts<\/code>. Below we add <code>accounts<\/code> as an attribute of class <code>Customer<\/code>, but first we show how to use <code>Array<\/code> by means of a ghost object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aGhost1: <strong>obj<\/strong>\n   accounts: <strong>obj<\/strong> Array(3, Account)\n   anAccountA, anAccountB : <strong>ref<\/strong> Account\n   JohnSmithProfile: <strong>obj<\/strong> Customer(\"John Smith\")\n   anAccountA := Account(JohnSmithProfile) \n   accounts.put(anAccountA):at&#91;2]\n   anAccountB := accounts.get&#91;2]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first item in the example above declares an <code>Array<\/code>-object with 3 elements (the first parameter) of type <code>Account<\/code> (the second parameter).<\/li>\n\n\n\n<li>The statement <code>accounts.put(anAccountA):at[2]<\/code> inserts the <code>Account<\/code> referred to by <code>anAccountA<\/code> as the second element in the array.<\/li>\n\n\n\n<li>The statement <code>anAccountB := accounts.get[2]<\/code> assigns the second element in the array to the reference <code>anAccountB<\/code>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">The situation at the end of <code>aGhost1<\/code> is shown in the snapshot:<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"679\" height=\"505\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountsArrayExample-1.jpg\" alt=\"\" class=\"wp-image-10411\" style=\"width:546px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountsArrayExample-1.jpg 679w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountsArrayExample-1-300x223.jpg 300w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><figcaption class=\"wp-element-caption\">Accounts Array Example<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">An index may in general be an expression that evaluate to an integer value as shown in the ghost object below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aGhost2: <strong>obj<\/strong>\n   accounts: <strong>obj<\/strong> Array(3, Account)\n   anAccountA, anAccountB, anAccountC : <strong>ref<\/strong> Account\n   inx: <strong>var<\/strong> integer\n   ... -- some code assigning references to anAccountA and anAccountB\n   inx := 1\n   accounts.put(anAccountA):at&#91;inx]\n   accounts.put(anAccountB):at&#91;inx + 1]\n   inx := 3\n   anAccountC := accounts.get&#91;inx - 2]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The object <code>aGhost2<\/code> has an <code>Array<\/code> <code>accounts<\/code> with 3 elements of type <code>Account<\/code>, three reference variables <code>anAccountA<\/code>, <code>anAccountB<\/code>, and an <code>AccountC<\/code>, and an integer variable <code>inx<\/code>.<\/li>\n\n\n\n<li>The three dots <code>...<\/code> stand for code not shown &#8211; see chapter <script>mkRef(\"Notation\");<\/script>.<\/li>\n\n\n\n<li>The variable <code>inx<\/code> assigned the value 1.<\/li>\n\n\n\n<li>The expression <code>accounts.put(anAccountA):at[inx]<\/code> assigns the reference hold by <code>anAccountA<\/code> to element no. 1 in <code>accounts<\/code>, since the value of <code>inx<\/code> is 1.<\/li>\n\n\n\n<li>The expression <code>accounts.put(anAccountB):at[inx + 1]<\/code> assigns the reference hold by <code>anAccountB<\/code> to element no. 2 in <code>accounts<\/code>, since expression <code>inx + 1<\/code> has the value 2.<\/li>\n\n\n\n<li>The statement <code>anAccountC := accounts.get[inx - 2]<\/code> assigns element no. 1 in <code>accounts<\/code> to <code>anAccountC<\/code> since expression <code>inx - 2<\/code> has the value 3 (as <code>inx<\/code> is assigned the value 3 before the statement) &#8211; <code>anAccountC<\/code> will thus refer to the same object as <code>anAccountA<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If the index expression is not within the range of the array &#8211; here 1-3, the program execution will be terminated (aborted) with an error message saying that there is an index error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may now extend class <code>Customer<\/code> with an array keeping track of the accounts of the customer: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer(name: <strong>var<\/strong> String): \n  <mark style=\"background-color:rgba(0, 0, 0, 0);color:#484848\" class=\"has-inline-color\"> addr: <strong>var<\/strong> String \n   email: <strong>var<\/strong> String<\/mark>\n   maxNoOfAccounts: <strong>val<\/strong> 10\n   noOfAccounts: <strong>var<\/strong> integer\n   accounts: <strong>obj<\/strong> Array(maxNoOfAccounts, Account)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the example, we assume that a customer may have at most 10 accounts as represented by the constant integer value <code>maxNoOfAcounts<\/code>. The integer variable <code>noOfAccounts<\/code> holds the number of accounts of the customer. An integer variable like <code>noOfAccounts<\/code> has initially the value 0 (zero).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next we add a method <code>addAccount<\/code> to class <code>Customer<\/code>: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer(name: <strong>var<\/strong> String):\n   -\"-\n   addAccount(acc: <strong>ref<\/strong> Account):\n      noOfAccounts := noOfAccounts + 1\n      if (noOfAccounts &lt;= maxNoOfAccounts) :then\n         accounts.put(acc):at&#91;noOfAccounts]\n      :else\n         console.print(\u201cCannot add Account\u201d) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method has a parameter <code>acc<\/code> referring to the <code>Account<\/code> to be added. An <code>if:then:else<\/code> statement is used to test if it is possible to add an account or if the limit of the maximum number of accounts has been reached in which case a message is printed on the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if:then:else<\/code> statement is similar to the <code>if:then<\/code> statement as explained in section <script>mkRef(\"Method returning a value\")<\/script>. The part after else is executed if the condition is false. For a more detailed description see section <script>mkRef(\"Statements\")<\/script> on statements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next we add a method that calculates the sum of the balance on all accounts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer(name: <strong>var<\/strong> String):\n   -\"-\n   balanceSum -&gt; bal: <strong>var<\/strong> float:\n      for (1):to(noOfAccounts):repeat\n          bal := bal + accounts.get&#91;inx].balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method makes use of a <em>for-statement<\/em>, <code>for:to:repeat<\/code> &#8211; also called <em>for-loop<\/em>, that iterates through the elements of the array. For each value in the interval <code>1..noOfAccounts<\/code>, the statement<code> bal := bal + accounts.get[inx].balance<\/code> is executed. The variable <code>inx<\/code> has the value 1 in the first execution 2 in the second and so one and <code>noOfAccounts<\/code> in the last execution. For a more detailed description see section <script>mkRef(\"Statements\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The complete new version of class <code>Customer<\/code> is shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer(name: <strong>var<\/strong> String):  \n   addr: <strong>var<\/strong> String \n   email: <strong>var<\/strong> String\n   maxNoOfAccounts: <strong>val<\/strong> 10\n   noOfAccounts: <strong>var<\/strong> integer\n   accounts: <strong>obj<\/strong> Array(maxNoOfAccounts,Account)\n\n   addAccount(acc: <strong>ref<\/strong> Account):\n      noOfAccounts := noOfAccounts  + 1\n      if (noOfAccounts &lt;= maxNoOfAccounts) :then\n         accounts.put(acc):at&#91;noOfAccounts]\n      :else\n         console.print(\u201cCannot add Account\u201d)\n      \n   balanceSum -&gt; bal: <strong>var<\/strong> float:\n      for(1):to(noOfAccounts):repeat\n          bal := bal + accounts.get&#91;inx].balance <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Value <code>Array<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>Array<\/code> may also hold a collection of an indexed sequences of values. The following example shows the declaration of an Array holding integer values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SQ: <strong>obj<\/strong> Array(5,integer) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This <code>Array<\/code> may hold 5 <code>integer<\/code> values. The name of the array is <code>SQ<\/code>, which is an abbreviation of squares and the code below thus stores the square of the index of a given element in the <code>Array<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (1):to(5):repeat\n   put(inx * inx):at&#91;inx]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Array literal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An <em>array literal<\/em> is an expression for specifying an <code>Array<\/code> object. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>SQ<\/code> in the previous section, may thus be assigned an <code>Array<\/code> object similar to the one computed by the above <code>for:to:repeat<\/code> loop using an array literal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SQ := (1,4,9,25)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>(1,4,9,25)<\/code> is an example of an array literal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next example shows the use of array literals for arrays holding references:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>accounts: <strong>obj<\/strong> Array(3,Account)\nacc1,acc2: <strong>ref<\/strong> Account\n... -- statements assigning values to acc1 and acc2\naccounts := (acc1, acc2, Account(\"Simon Jones\"))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The array literal <code>(acc1,acc2,Account(\"Simon Jones\")) <\/code>creates an <code>Array<\/code> object holding references to the <code>Accounts<\/code> referred to by <code>acc1<\/code> and <code>acc2<\/code> and a reference to a new <code>Acount<\/code> object generated by <code>Account(\"Simon Jones\")<\/code>.<\/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=1849\" 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 customer may have more than one account. In this section, we extend class Customer to keep track of the accounts of a given customer. We may use class Set for this purpose, but we use this example to introduce Array, which is another common collection object. An Array-object is an indexed sequence of references [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":469,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1849","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\/1849","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=1849"}],"version-history":[{"count":117,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1849\/revisions"}],"predecessor-version":[{"id":10734,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1849\/revisions\/10734"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/469"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}