{"id":6562,"date":"2024-08-02T13:01:25","date_gmt":"2024-08-02T11:01:25","guid":{"rendered":"https:\/\/oopm.org\/?page_id=6562"},"modified":"2025-05-09T12:24:37","modified_gmt":"2025-05-09T10:24:37","slug":"2-4-the-first-program","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=6562","title":{"rendered":"2.4 The first program"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages6562&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=wpv2pages6562&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 description written in a programming language is called a <em>computer program<\/em> or just a <em>program<\/em> but is also referred to as <em>source code<\/em> or simply <em>code<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The description of <code>account_1010<\/code> in the previous section may be considered an example of a program, although it is not very useful since it is not used for any purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here we show an example of a program where money is deposited and withdrawn, represented as actions changing the balance of <code>account_1010<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may assign values to&nbsp;<code>balance<\/code>&nbsp;by an using assignment statements as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>account_1010.balance := 350.56<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>account_1010.balance<\/code> access the attribute <code>balance<\/code> of the object <code>account_1010<\/code> and is an example of an <em>attribute accessor<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Execution of this statement implies that <code>balance<\/code> in <code>account_1010<\/code> now holds the value 350.56.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operator&nbsp;<code>:=<\/code>&nbsp;specifies that the variable&nbsp;<code>account_1010.balance<\/code>&nbsp;on the left-hand side gets a new value as specified by the <em>expression<\/em> 350.56 at the right-side of&nbsp;<code>:=<\/code>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The operation described by the assignment statement may represent a deposit of 350.56 on John Smiths account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The assignment statement as shown above is just a program fragment and must be placed in an object in order to be executed. The next example shows such an object &#8211; our first example of a <em>program<\/em> carrying out operations:<\/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<pre class=\"wp-block-code\"><code>myFirstProgram: <strong>obj<\/strong> \n   account_1010: <strong>obj<\/strong>\n      owner: <strong>val<\/strong> \"John Smith\"\n      balance: <strong>var<\/strong> float \n   account_1010.balance := 350.56<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">As said, a <em>program<\/em> is an object descriptor like the one for <code>myFirstProgram<\/code>. The opposite is not the case since an object descriptor is not necessarily a program. We return to this in chapter <script>mkRef(\"Modules\")<\/script>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This program is also an object descriptor that consist of the following items:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The declaration of the object <code>account_1010<\/code>.<\/li>\n\n\n\n<li>The assignment statement <code>account_1010.balance := 350.56<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The object descriptor of <code>myFirstProgram<\/code> may be executed by a computer and this implies the following actions in that order:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The object <code>myFirstProgram<\/code> is generated.<\/li>\n\n\n\n<li>The object <code>account_1010<\/code> is generated.<\/li>\n\n\n\n<li>The assignment <code>account_1010.balance := 350.56<\/code> is executed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To illustrate what goes on during a program execution we use <em>snapshots<\/em> of (part of) the state of the program execution. The following figures illustrates what goes on at selected points of execution<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\">Below the right columns contain <em>object diagrams<\/em> that shows the stage of the <code>account_1010<\/code> object at a given point of time during program execution. The left column shows the program text marked with a red arrow (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">&#8211;&gt;<\/mark>) that shows the next statement to be executed at the time of the snapshot. If the arrow is pointing beyond the last statement, the next action to be executed is termination of the object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first snapshot shows the situation when <code>account_1010<\/code> has been generated and before execution of the assignment:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>owner<\/code> attribute of <code>account_1010<\/code> has the value <code>\"John Smith\"<\/code> since it is declared as constant using the keyword <code><strong>val<\/strong><\/code>.<\/li>\n\n\n\n<li>The <code>balance<\/code> attribute has the value 0 (zero), which is the default value for data-items of type <code>float<\/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<pre class=\"wp-block-code\"><code>   myFirstProgram: <strong>obj<\/strong>\n      account_1010: <strong>obj<\/strong>\n         owner: <strong>val<\/strong> \"John Smith\"\n         balance: <strong>var<\/strong> float \n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>   account_1010.balance := 350.56<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"240\" height=\"106\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.4-account_1010-I.jpg\" alt=\"\" class=\"wp-image-7694\" style=\"width:188px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The next snapshot shows the situation after execution of the assignment statement:<\/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<pre class=\"wp-block-code\"><code>   myFirstProgram: <strong>obj<\/strong>\n      account_1010: <strong>obj<\/strong>\n         owner: <strong>val<\/strong> \"John Smith\"\n         balance: <strong>var<\/strong> float \n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>   account_1010.balance := 350.56\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"242\" height=\"105\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.4-account_1010-II.jpg\" alt=\"\" class=\"wp-image-7696\" style=\"width:194px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The data-item <code>balance<\/code> now holds the value 350.56.<\/li>\n\n\n\n<li>The point of execution (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">&#8211;&gt;<\/mark>) is at the end of <code>myFirstProgram<\/code> pointing to an implicit action that when executed terminates execution of the object.<\/li>\n<\/ul>\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=6562\" 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 description written in a programming language is called a computer program or just a program but is also referred to as source code or simply code. The description of account_1010 in the previous section may be considered an example of a program, although it is not very useful since it is not used for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":175,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6562","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\/6562","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=6562"}],"version-history":[{"count":37,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/6562\/revisions"}],"predecessor-version":[{"id":11373,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/6562\/revisions\/11373"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/175"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}