{"id":3416,"date":"2024-02-09T11:07:26","date_gmt":"2024-02-09T10:07:26","guid":{"rendered":"https:\/\/oopm.org\/?page_id=3416"},"modified":"2024-12-20T09:16:38","modified_gmt":"2024-12-20T08:16:38","slug":"6-2-statements","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=3416","title":{"rendered":"6.2 Statements"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3416&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=wpv2pages3416&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\" id=\"ref:Statements\">A statement describes a computation. Execution of the statement implies<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>that the computation is performed by the computer. Such a computation may be a simple operation or consists of a sequence of operations. We have the following kind of statements: assignment statements, conditional statements, loop statements, break statements, method invocations, and object instantiation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conditional statements, loop statements, and break statements are often referred to as <em>control structures<\/em> since they control the order in which the\u00a0statements\u00a0of a program are executed based on the state of the objects &#8211; this is often referred to as the <em>control flow <\/em>of the program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some of the statements are primitives of the qBeta language and others are defined in the qBeta library. We return to this is in the last section on this chapter.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The assignment statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the previous chapters, the <em>assignment statement<\/em> has been used to describe operations carried out by methods like <code>addInterest<\/code>, <code>deposit<\/code> and <code>withdraw<\/code>. The assignment statement has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>VariableDataItem<\/em> := <em>Expression<\/em> <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The left side of the statement must be the name of a variable data-item such as <code>balance<\/code> defined within the various descriptions of accounts. A variable data-item is declared using <code><strong>var<\/strong><\/code> or <code><strong>ref<\/strong><\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The right-side of the statement is an <em>expression<\/em> which describes the computation of a datum. An expression may be an arithmetic expression like <code>balance - amount<\/code>, as described in the previous section. This expression evaluates to a primitive value of type float. The statement below assigns this value to the variable <code>balance<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>balance := balance + amount<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conditional statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes the computations of a method depends on the values of data-items of the object or other conditions. One example is <code>withdraw<\/code> where you may not be able to withdraw an amount that is greater than the balance. We may describe this using a conditional statement like an <code>if:then<\/code> statement or an <code>if:then:else<\/code>-statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The if:then statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>if:then<\/code> statement describes a conditional execution of a sequence of items. An example of an <code>if:then<\/code> statement is shown in section <script>mkRef(\"Method returning a value\");<\/script>. It has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (<em>condition<\/em>) :then\n   <em>thenPart<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>condition<\/em> is a boolean expression that evaluates to ether true or false. The <em>thenPart<\/em> clause consists of a sequence of items and as mentioned, an item may be a declaration or a statement. The <em>thenPart<\/em> is only executed if the <em>condition<\/em> evaluates to true.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The if:then:else statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>if:then:else<\/code> statement describes a conditional execution selecting between two sequences of items and it has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (<em>condition<\/em>) :then\n   <em>thenPart<\/em>\n:else\n   <em>elsePart<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As for the <code>if:then <\/code>statement, the <code>condition<\/code> must be an expression that evaluates to the Boolean value true or false. The clauses <code><em>thenPart<\/em><\/code> and <em><code>elsePart<\/code><\/em> are both a sequence of items. If the condition evaluates to true, then the items describe by <em><code>thenPart<\/code><\/em> are executed. Otherwise the items described by <em><code>elsePart<\/code><\/em> are executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of an <code>if:then:else<\/code> statement is given in section <script>mkRef(\"Array and for-loop\")<\/script> where the body of <code>withdraw<\/code> has an <code>if:then:else<\/code> statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   balance: <strong>var<\/strong> float;\n   ...\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      if (amount &lt;= balance) :then\n         balance := balance - amount\n      :else\n         console.print(\"The balance is less than the amount\")\n      newB := balance <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> In the example, the <code>condition<\/code> is <code>amount &lt;= balance<\/code> where <code>&lt;=<\/code> means less-than-or-equal. This means that <code>amount &lt;= balance <\/code>evaluates to true if <code>amount<\/code> is less-than-or-equal to <code>balance<\/code>. Otherwise it evaluates to false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example, <em><code>thenPart<\/code><\/em> is an assignment statement as in the previous example of <code>withdraw<\/code>. The <em><code>elsePart<\/code><\/em> is a statement that prints the <code>String<\/code> <code>\"The balance is less than the amount\"<\/code> on a console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We assume that our system has an object <code>console<\/code> that represents a window on the screen of the computer executing the bank system. We also assume that the <code>console<\/code> object has a <code>print<\/code> method that takes a <code>String<\/code> as an argument and prints it in the window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Loop statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A loop statement executes a list of statements forever or until some condition is met or interrupted by a break statement. We have three forms of loop statements <code>cycle<\/code>, <code>while<\/code>-loop and <code>for<\/code>-loop:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The cycle statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>cycle<\/code> statement has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cycle\n   <em>items<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <em><code>Statements<\/code><\/em> is a sequence of items. Execution of a <code>cycle<\/code> statement implies that <em><code>items<\/code><\/em> are executed forever unless interrupted by a possible <code>break<\/code> statement (see below) in <em><code>Items<\/code><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our bank system we assume that interest is added to each account on the first day of every month. To do this, the following cycle statement may be executed by some agent in the bank system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cycle\n   if (today.isFirstDayOfMonth) :then\n      JohnSmithsAccount.addInterest\n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above example is quite tentative. We have not specified what an agent might be &#8211; an agent may be a parallel process running in the bank system &#8211; we describe parallel processes in section <script>mkRef(\"Active objects and parallel programming\");<\/script>. Also the condition <code>today.isFirstDayOfMonth<\/code> is left unspecified. Some more immediately useful examples of <code>cycle<\/code> will be shown in the following.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The while-loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>while<\/code> statements repeatedly executes a list of statements as long as a given condition evaluates to true. It has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (<em>BooleanExp<\/em>) :repeat\n   <em>Items<\/em> <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <em><code>BooleanExp<\/code><\/em> is a boolean expression and <code><em>Items<\/em><\/code> is a list of statements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"String values\");<\/script> on the data type <code>String<\/code>, we have seen an example of a while-loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (aName.get&#91;i] &lt;&gt; ' ') :repeat\n    console.print(aName.get&#91;i])\n    i := i + 1 <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The for-loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>for<\/code> loop executes a list of statements a specific number of times. It has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tiny-font-size\"><code>for (StartValue):to(EndValue):repeat\n   <em>Items<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <em><code>StartValue<\/code><\/em> and <code>EndValue<\/code> are expressions that evaluate to integer values, and <em><code>Items<\/code><\/em> is a list of items. If <em><code>StartValue<\/code><\/em> evaluates to <code>n1<\/code> and <em><code>EndValue<\/code><\/em> evaluates to <code>n2<\/code>, then the <em><code>Items<\/code><\/em> are evaluated <code>(n2 - n1 + 1) <\/code>times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Within <em><code>Items<\/code><\/em>, the integer variable <code>inx<\/code> has the value of the current iteration number. That is, <code>inx<\/code> goes through the values <code>n1<\/code>, <code>n1 + 1<\/code>, <code>n1 + 1<\/code>, &#8230; , <code>n2<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"Using Array and for-loop\");<\/script>, the following example makes use of a for loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>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\">If <code>noOfAccounts<\/code> evaluates to the value 3, the statement in the for-loop is executed 3 times and <code>inx<\/code> goes through the values 1, 2, and 3. The effect is that the following statements are executed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bal := bal + accounts.get&#91;1].balance\nbal := bal + accounts.get&#91;2].balance\nbal := bal + accounts.get&#91;3].balance<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Break statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <em>break-statement<\/em> transfers the control-flow to another point in the program and is also called a <em>jump-statement<\/em>. The flow of control may be to the beginning or end of the current method object or the beginning or end of an enclosing method object or object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have two kinds of break-statements <code>leave<\/code> and <code>restart<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The leave statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>leave<\/em> statement has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>leave (<em>AttributeName<\/em>)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <em><code>AttributeName<\/code><\/em> is the name of an enclosing class, method or singular object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>L<\/code> is the name of an enclosing attribute, then <code>leave(L)<\/code> will transfer control to the end of the statement-part of <code>L<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As an example, we may make a more safe version of the method for printing the first name in a given string by testing that there is actually a blank character in the <code>String<\/code> and if not leave the while-loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>L:\n   while (aName.get&#91;i] &lt;&gt; ' ') :repeat\n       console.print(aName.get&#91;i])\n       i := i + 1\n       if (i &gt; aName.length) :then\n          console.print(\"No blank char in String\")\n          leave(L)<\/code><\/pre>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">Here we have given the while-loop the name <code>L<\/code> id we get at the end of <code>aName<\/code> without meeting blank, <code>leave(L)<\/code> will terminate the while-loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The restart statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>restart<\/em> statement has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>restart (<em>AttributeName<\/em>)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <em><code>AttributeName<\/code><\/em> is the name of an enclosing class, method or singular object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>L<\/code> is the name of an enclosing attribute, then <code>restart(L)<\/code> will transfer control to the beginning of the statement-part of <code>L<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may use <code>restart<\/code> to write another variant of the first name in the <code>String<\/code> <code>aName<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>firstName:\n   i: <strong>var<\/strong> integer\n   i := i + 1\n   if (i &lt;= aName.length) :then\n      if (aName.get&#91;i] &lt;&gt; ' ') :then\n         console.print(aName.get&#91;i])\n         restart(firstName)\n   :else\n      console.print(\"No space in String\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The inner-most <code>if:then<\/code>-statement prints the next character in a possible first name and then restart execution from the beginning of <code>firstName<\/code>. All local data-items of <code>firstName<\/code> &#8211; in this case the variable <code>i<\/code>, keeps its values, so <code>i<\/code> is incremented in each iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method invocation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned in the previous section, a <em>method invocation<\/em> may be part of an expression. It may, however, also just appear as a statement in case no value is returned by the invocation or if the value is not used:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  account_1010.addInterest<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>account_1010.addInterest<\/code> is an example of a method invocation being used as a statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For further descriptions of object instantiation and method invocation, see chapter&nbsp;<script>mkRef(\"Class and method details\");<\/script> and chapter&nbsp;<script>mkRef(\"Generation of objects\");<\/script>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Object instantiation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An object instantiation may in principle also be used as a statement although it in most cases appears as an expression since the resulting reference is assigned to a reference variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Do objects<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This section is a reference section and may be skipped during a first reading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is sometimes useful to be able to group statements in a block of program text that has a name that can be referred to from e.g. break statements like leave and restart. A <code>do<\/code>-object may be used for this purpose and has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>Name<\/em>: <strong>do<\/strong> <em>SuperMethod<\/em>\n   <em>Items<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>do<\/code>-object is executed as a statement. It differs from a data-item defined using <code>obj<\/code> in the sense that attributes in a <code>do<\/code>-object cannot be accessed and it is not possible to obtain a reference to a <code>do<\/code>-object.<mark style=\"background-color:rgba(0, 0, 0, 0);color:#458250\" class=\"has-inline-color\"> <\/mark>A <code>do<\/code> object is thus a singular method object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following is a sketchy example of using a <code>do<\/code>-object<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aGhost: <strong>obj<\/strong>\n   ...\n   L: <strong>do<\/strong>\n      ...\n      if (aCondition) :then\n         leave (L)\n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the execution of <code>aGhost<\/code> reaches <code>L<\/code>, the <code>do<\/code> object is executed. If the <code>aCondition<\/code> evaluates to true, <code>leave(L)<\/code> implies that execution of <code>L<\/code> is terminated and execution continues after <code>L<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">On defining control abstractions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Just a few of the statement types described above are primitives of the qBeta language. These include assignment, the <code>if:then<\/code> statement and the break statements. All the other statement types described are defined in the qBeta library. The user of qBeta may in a similar way define his or her own control statements, defined by methods, which we here refer to as&nbsp;<em>control abstractions<\/em>. This is described in chapter <script>mkRef(\"Control structure methods\");<\/script>. In this chapter, we also explain that most of the above control statements in fact describe invocations of <em>singular method objects<\/em> similar to singular data-objects declared using <code><strong>obj<\/strong><\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/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=3416\" 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 statement describes a computation. Execution of the statement implies that the computation is performed by the computer. Such a computation may be a simple operation or consists of a sequence of operations. We have the following kind of statements: assignment statements, conditional statements, loop statements, break statements, method invocations, and object instantiation. Conditional statements, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":598,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3416","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\/3416","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=3416"}],"version-history":[{"count":126,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3416\/revisions"}],"predecessor-version":[{"id":10687,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3416\/revisions\/10687"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/598"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}