{"id":3413,"date":"2024-02-09T11:05:24","date_gmt":"2024-02-09T10:05:24","guid":{"rendered":"https:\/\/oopm.org\/?page_id=3413"},"modified":"2024-11-02T17:28:01","modified_gmt":"2024-11-02T16:28:01","slug":"6-1-expressions","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=3413","title":{"rendered":"6.1 Expressions"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3413&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=wpv2pages3413&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\">An expression is a combination of one or more constants, variables, operators and functions in the form of method invocations. The <em>evaluation<\/em> of an expressions yields a <em>datum<\/em>, which may be a value or a reference.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>We use the term evaluation for execution of an expression. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value expressions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One kind of expression is a sequence of digits representing a number like 10, -12, 3.14, and 1.3E4. The numbers 10 and -12 represent integer numbers whereas 3.14 and 1.3E4 represent float numbers where a dot (&#8216;.&#8217;) may be used to specify the decimal point and the &#8216;E&#8217; specifies a possible exponential art. A representation of a number in this way is often called a <em>literal<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Evaluation of a number like 10 of course yields the value 10, and evaluation of 3.14 of course yields the corresponding float value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may combine numbers into <em>compound expressions <\/em>using binary operators as in: 111 + 23, 3 * 12, and 0.1 * 111.12. Evaluation of such a compound expression consists of applying the operator to its operands: 111 + 23 thus yields the value 134. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A data-item representing a number may also be used as an expression. If <code>anInt<\/code> is a constant or variable integer, then <code>anInt<\/code> may be used in an expression that evaluates to the value that <code>anInt<\/code> is currently holding.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method invocation returning a value may also be used as an expression. An example is <code>anAccount.withdraw(140)<\/code>. Here <code>withdraw<\/code> returns the value of the <code>balance<\/code> of <code>anAccount<\/code> after withdrawal of 140.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Expressions may in general be combined into arbitrary expressions like <code>anInt * 10 + max(a,b) * 4<\/code>, where max is a method returning the larger of its two arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For compound expressions the order of evaluation of is subexpressions is important. Consider <code>3 + 5 * 2<\/code>. The standard way to evaluate this expression is to evaluate <code>5 * 2<\/code> and add the result to <code>3<\/code> yielding <code>13<\/code>. We thus have to be aware of <em>priorities<\/em> between operators.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operator <code>^<\/code>(exponentiation) has highest priority, then comes multiplication (<code>*<\/code>) and division (<code>\/<\/code>), and addition (<code>+<\/code>) and subtraction (<code>-<\/code>) have the lowest priority<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-secondary-color\">.<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For operators of the same priority one must be aware of whether an expression is evaluated from left to right or right to left. For one operators like + and * it does not make a difference, but an operator like &#8211; it makes a difference. For an expression <code>10-6-3<\/code>; if you evaluate from left you get <code>(10-6)-3 = 1<\/code> whereas from right you get <code>10-(6-3) = 7.<\/code> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Parentheses <\/em>may be used to control the evaluation of subexpressions as in: <code>(3 + 5) * 2<\/code>. Here the parentheses means that <code>3 + 5<\/code> is evaluated yielding the value <code>8<\/code> which then is multiplied to <code>2<\/code> yielding <code>16<\/code>. For a newcomer, it may be a good idea to use extra parentheses if in doubt about how a givne expression is evaluated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rules for priority, associativity and parentheses we use in this book are the ones generally adapted mathematics and most programming languages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An expression has a <em>type<\/em> being the type of the value yielded by evaluation of the expression. In the above examples, we have seen expressions of type integer and float.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above kind of expressions using integer numbers and float numbers are called arithmetic expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We also have <em>relational expressions <\/em>like <code>a &lt; b<\/code>. The <em>relational operator<\/em> <code>'&lt;'<\/code> tests whether or not <code>a<\/code> it is less than <code>b<\/code>. It evaluates to the Boolean value <code>true<\/code> or <code>false<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A third kind of expressions are boolean expressions where the operands are boolean values and the operators are logical operators like <code>&amp;&amp;<\/code> (and), <code>||<\/code> (or), etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The condition of a conditional statement like if-then must be a boolean expression as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ((a &lt; 10) &amp;&amp; (b = 100)) :then\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The expression (<code>a &lt; 10<\/code>) evaluates to one of the Boolean values <code>true<\/code> or <code>false<\/code> and so do (<code>b = 100<\/code>). The Boolean results of these two expressions are then evaluated using the Boolean and-operator <code>&amp;&amp;<\/code>, which also yields either <code>true<\/code> or <code>false<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Operators<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The table below show the possible operators that may be used in expressions:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Arithmetic operators<\/td><td>+, -, *, \/, ^ (exponentiation)<\/td><\/tr><tr><td>Relational operators<\/td><td>=, &lt;&gt;, &lt;=, &lt;, &gt;, &gt;=<\/td><\/tr><tr><td>Boolean operators<\/td><td>&amp;&amp;, ||, not<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Operators<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The above expression consists of numbers and operators with numbers as operands.  Characters and Strings may also be used as expressions and the resulting types is then of type char or String. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Character<\/em> <em>literals<\/em> like &#8216;a&#8217;, &#8216;9&#8217;, &#8216;?&#8217;, etc.  may also be used as expressions yielding the characters &#8216;a&#8217;, &#8216;9&#8217;, &#8216;?&#8217;, etc.  &#8211; as of no surprise.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A <em>String literal<\/em> like <code>\"Hello\"<\/code> may be used as an expression and <code>\"Hello \" + \"world!\"<\/code> is an expression yielding the string <code>\"Hello world!\"<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference expressions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An expression may also yield a reference as explained in section 5.1, but we repeat it here for completeness. One form of a reference expression is the name of a data-item declared as a reference using <strong><code>obj<\/code><\/strong> or <strong><code>ref<\/code><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c1: <strong>ref<\/strong> Customer\nc2: <strong>obj<\/strong> Customer\nc1 := c2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the <code>c2<\/code> on the right-side of the assignment is an example of such a reference expression. And it of course evaluates to a reference to the <code>Customer<\/code> object currently referred to by <code>c2<\/code>. After the assignment, <code>c1<\/code> refers to the same <code>Customer<\/code> object as <code>c2<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another form of a reference expression is instantiation of an object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c1 := Customer(\"John Smith\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>Customer(\"John Smith\") <\/code>is an example of a reference expression that when evaluated generates a new <code>Customer<\/code> object and the resulting value is a reference to this new object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The <code>this<\/code> reference<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose that we want to notify the owner of an <code>Account<\/code> whenever there has been a transaction on his\/her account. To represent this, we may add a notify method 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   notify(acc: <strong>ref<\/strong> Account, amount: <strong>var<\/strong> float):\n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notify has two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>acc<\/code> is a reference to the <code>Account<\/code> where there has been a transaction.<\/li>\n\n\n\n<li><code>amount<\/code> is the value that has been deposited or withdrawn.  A positive value for <code>amount<\/code> represents a <code>deposit<\/code> and a negative value represents a <code>withdraw<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We may then invoke this method from <code>withdraw<\/code> and <code>deposit<\/code> t using an invocation like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>owner.notify(theAccount,amount)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <code>theAccount<\/code> is supposed to be a reference to the <code>Account<\/code> object where <code>deposit<\/code> or <code>withdraw<\/code> has been <code>invoked<\/code>. But so far, we have no such reference.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may, however, use the expression <code>this(Account)<\/code> within <code>deposit<\/code> and withdraw. It evaluates to a reference to the enclosing <code>Account<\/code> object. We may thus use <code>this(Account)<\/code> in class <code>Account<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   -\"-\n   deposit(amount: <strong>var<\/strong> float):\n      -\"-\n      owner.notify(this(Account),amount)\n   withdraw(amount: <strong>var<\/strong> float):\n      -\"-\n      owner.notify(this(Account),- amount)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Object instantiation and method invocations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In general object instantiation\/generation and method invocations may be elements of an expression. The previous section has an example of an object generation using <code>Customer(\"John Smith\")<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method invocation as an expression is shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>newBalance: <strong>var<\/strong> float\nnewBalance := anAccount.withdraw(200)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>anAccount.withdraw(200)<\/code> is an example of a method invocation used as an expression returning the new balance of the <code>anAccount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For further descriptions of object instantiation and method invocation, see chapter <script>mkRef(\"Class and method details\");<\/script> and chapter <script>mkRef(\"Generation of objects\");<\/script>.<\/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=3413\" 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>An expression is a combination of one or more constants, variables, operators and functions in the form of method invocations. The evaluation of an expressions yields a datum, which may be a value or a reference. We use the term evaluation for execution of an expression. Value expressions One kind of expression is a sequence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":598,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3413","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\/3413","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=3413"}],"version-history":[{"count":34,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3413\/revisions"}],"predecessor-version":[{"id":9205,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3413\/revisions\/9205"}],"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=3413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}