{"id":3392,"date":"2024-02-08T13:20:15","date_gmt":"2024-02-08T12:20:15","guid":{"rendered":"https:\/\/oopm.org\/?page_id=3392"},"modified":"2024-12-29T16:48:55","modified_gmt":"2024-12-29T15:48:55","slug":"5-2-value-data-items","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=3392","title":{"rendered":"5.2 Primitive values"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3392&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=wpv2pages3392&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\">In this section, we further describe data-items that hold primitive values. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Floating point values<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Data-items such as&nbsp;<code>balance<\/code>&nbsp;and&nbsp;<code>interestRate<\/code>&nbsp;are examples of data-items holding values of<em> float value types<\/em>, which represents a subset of the real numbers. These data-items are declared as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-border-color has-tertiary-border-color has-tiny-font-size\" style=\"border-width:1px\"><code>balance: <strong>var<\/strong> float\ninterestRate: <strong>var<\/strong> float<\/code><\/pre>\n\n\n\n<style>\n#XXX {\n  width: 40%;\n  padding-left: 15px;\n  margin-left: 15px;\n  float: right;\n  background-color: lightgray;\n  font-size: 0.8em; \n}\n<\/style>\n<p id=\"XXX\">The set of real numbers is infinite, and a computer can only represent a subset of the real values. This subset is called <i>floating point values<\/i>.\u00a0Since only a subset can be represented, the evaluation of floating point expressions often imply that some values are rounded to a value that the computer can represent. For calculations of a large amount of numbers these roundings may lead to misleading and erroneous results. It is therefore recommended that people writing programs involving floating point calculations learn about how to avoid rounding errors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The specification <code><strong>var<\/strong> float<\/code> in the declarations of <code>balance<\/code> and <code>interestRate<\/code> specifies that these may hold floating point values, and that their type is <code>float<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples of float values are: 100, 3.14, 0.56, -12.11, <code>13.5E7<\/code>, and <code>1.11E-7<\/code>. The <code>'E'<\/code> in 1<code>3.5E7 <\/code>means that the value is 13.7 multiplied by 10<sup>7<\/sup>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The keyword <code><strong>var<\/strong><\/code> used in the declaration specifies that these data-items are variable, which means that they may be assigned different values during the execution of the program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>balance := 500\ninterestRate := 3.07\n...\nbalance := balance + amount\ninterestRate := 2.9\n...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to define a float constant using the keyword <strong><code>val<\/code><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tiny-font-size\"><code>pi: <strong>val<\/strong> 3.14<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The default value for a <code>float<\/code> variable is zero (<code>0.0<\/code>).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Integer values<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In section 3.4 <code>maxNoOfAccounts<\/code> and <code>noOfAccounts<\/code> of class <code>Customer<\/code> are examples of data-items holding integer values. <\/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)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Integer<\/code> is another example of a primitive value type. Examples of <code>Integer<\/code> values are 17, 111, -4, 0, and -1014.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As for <code>float<\/code>, an <code>Integer<\/code> may be declared as a constant or a variable. In the above example, <code>maxNoOfAccounts<\/code> is a constant and <code>noOfAccounts<\/code> is a variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The data-item&nbsp;<code><code>noOfAccounts<\/code><\/code> is an example of a variable that may hold different integers during the program execution, while the data-item&nbsp;<code><code>maxNoOfAccounts<\/code><\/code> on the other hand is constant. i.e. holding the value <code>10<\/code> during the whole programm execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default value for an <code>integer<\/code> variable is zero (<code>0<\/code>).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Boolean values<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In section 3.4 the method <code>addAccount<\/code> of <code>Customer<\/code> has an if-then-else-statement:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tiny-font-size\"><code>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 expression <code>noOfAccounts &lt;= maxNoOfAccounts<\/code> evaluates to one of the values <code>True<\/code> or <code>False<\/code>, which both are <code>Boolean<\/code> values. <code>Boolean<\/code> is another example of a primitive type and <code>True<\/code> and <code>False<\/code> are the only values of type <code>Boolean<\/code>. We may declare a data-time of type <code>Boolean<\/code> as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;b1:&nbsp;<strong>val<\/strong>&nbsp;True\n&nbsp;&nbsp;&nbsp;b2:&nbsp;<strong>var<\/strong>&nbsp;Boolean<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data-item&nbsp;<code>b1<\/code>&nbsp;is a constant having the value <code>True<\/code> during the whole program execution. The data-item&nbsp;<code>b2<\/code>&nbsp;is a variable that may hold the values <code>True<\/code> or <code>False<\/code> at different points during the program execution. In the next example,&nbsp;<code>b2<\/code>&nbsp;is assigned the value <code>False<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code has-tiny-font-size\"><code>&nbsp;&nbsp;&nbsp;b2&nbsp;:=&nbsp;False<\/code><\/pre>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">We may add a boolean <code>isClosed<\/code> to class <code>Account<\/code> to represent whether or not the account is open or closed. An alternative may be to add a <code>closingDate<\/code> for the account and then make <code>isClosed<\/code> a method returning a boolean which is true if <code>closingDate &lt;= thisDate <\/code>and false otherwise.<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 default value for a <code>boolean<\/code> variable is the value <code>false<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Character values<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>String<\/code> consists of characters from some character set like UniCode, Latin1, etc. The type <code>char<\/code> may be used to declare a data-item holding a value representing a character.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A character may be a letter, a digit, a special character, a blank\/space or an end-of-line character. A character literal is typically written within single quotes like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;\u2018a\u2019,&nbsp;\u2018b\u2019,&nbsp;\u2018Q\u2019,&nbsp;'L\u2019&nbsp;&nbsp;\u2014&nbsp;examples&nbsp;of&nbsp;letters\n&nbsp;&nbsp;&nbsp;\u20180\u2019,&nbsp;7\u2032&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2014&nbsp;examples&nbsp;of&nbsp;digits\n&nbsp;&nbsp;&nbsp;\u2018.\u2019,&nbsp;\u2018:\u2019,&nbsp;\u2018+\u2019,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2014&nbsp;examples&nbsp;of&nbsp;special&nbsp;characters\n&nbsp;&nbsp;&nbsp;\u2018&nbsp;\u2018&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2014&nbsp;the&nbsp;blank&nbsp;character<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An end-of-line character has no graphical form and is thus denoted as <code>\u2018\\n\u2019<\/code>. The character <code>\u2018\\\u2019<\/code> is a so-called escape character the implies that the next character defines a special character. The following are examples of characters defined using the escape character<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;\u2018\\n\u2019&nbsp;&nbsp;\u2014&nbsp;end-of-line\n&nbsp;&nbsp;&nbsp;\u2018\\t\u2019&nbsp;&nbsp;\u2014&nbsp;tabulator\n&nbsp;&nbsp;&nbsp;\u2018\\\u201d&nbsp;&nbsp;&nbsp;\u2014&nbsp;the&nbsp;quote&nbsp;character&nbsp;\u2018\n&nbsp;&nbsp;&nbsp;...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The following example shows the declaration of data-items of type <code>char<\/code> and assignment statements:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&nbsp;&nbsp;&nbsp;ch1:&nbsp;<strong>val<\/strong>&nbsp;\u2018!\u2019\n&nbsp;&nbsp;&nbsp;ch2:&nbsp;<strong>var<\/strong>&nbsp;char\n&nbsp;&nbsp;&nbsp;ch2&nbsp;:=&nbsp;\u2018Q\u2019<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data-item&nbsp;<code>ch1<\/code>&nbsp;is a a constant holding the exclamation mark character<code>&nbsp;'!'<\/code>. The data-item&nbsp;<code>ch2<\/code>&nbsp;is a variable of type&nbsp;<code>char<\/code>. It may hold different character values during the program execution. Execution of the assignment statement&nbsp;<code>ch2 := 'Q'<\/code>&nbsp;has the effect that&nbsp;<code>ch2<\/code>&nbsp;holds the character&nbsp;<code>'Q'<\/code>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So far we have not seen examples using <code>char<\/code>, but such examples will be shown later in this book, see e.g. section <script>mkRef(\"Virtual methods\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default value for a <code>char<\/code> variable is the zero char, which is a character that has the integer value zero.<\/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=3392\" 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>In this section, we further describe data-items that hold primitive values. Floating point values Data-items such as&nbsp;balance&nbsp;and&nbsp;interestRate&nbsp;are examples of data-items holding values of float value types, which represents a subset of the real numbers. These data-items are declared as follows: The set of real numbers is infinite, and a computer can only represent a subset [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2270,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3392","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\/3392","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=3392"}],"version-history":[{"count":34,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3392\/revisions"}],"predecessor-version":[{"id":10838,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3392\/revisions\/10838"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2270"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}