{"id":7470,"date":"2024-09-13T10:11:42","date_gmt":"2024-09-13T08:11:42","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7470"},"modified":"2024-11-18T09:07:46","modified_gmt":"2024-11-18T08:07:46","slug":"9-2-conditional-statements","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7470","title":{"rendered":"9.2 Conditional statements"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7470&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=wpv2pages7470&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 conditional statement is a statement that execute different statements depending on the value of a boolean expression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">If:then<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if:then<\/code> statement used in previous sections is an example of a conditional statement. The code below shows a sketch of a definition of <code>if:then<\/code> as a control method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(cond: <strong>var<\/strong> boolean):then{thenPart:&lt; Object}:\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, <code>if:then<\/code> is a built-in primitive control method in qBeta and cannot be defined in terms of other language mechanism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining if:then:else<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if:then:else<\/code> is an example of a control method that may be define as method in qBeta. You may find the following definition in the qBeta library:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(cond: <strong>var<\/strong> boolean):then{thenPart:&lt; Object}\n                     :else{elsePart:&lt; Object}:\n   if (cond) :then  \n      thenPart\n      leave(if:then:else)\n   elsePart<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The control<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>method has the following structure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The name of the control method is <code>if:then:else<\/code>.<\/li>\n\n\n\n<li>It has three parameters, <code>cond<\/code>, which is a <code>boolean<\/code>, thenPart, which is a virtual <code>Object<\/code>, and <code>elsePart<\/code>, which also is a virtual <code>Object<\/code>.<\/li>\n\n\n\n<li>The parameters are specified using the fat-comma syntax as described in section <script>mkRef(\"Using fat comma for specifying parameters\")<\/script>.<\/li>\n\n\n\n<li>The first item in the main-part of the abstraction is an if-then statement.<\/li>\n\n\n\n<li>If the boolean <code>cond<\/code> is true then the virtual <code>thenPart<\/code> is executed followed by <code>leave(if:then:else),<\/code> which terminates the execution of <code>if:then:else<\/code>.<\/li>\n\n\n\n<li>If the boolean <code>cond<\/code> is false then the virtual <code>elsePart<\/code> is executed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Consider an invocation of <code>if:then:else<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (amount &lt;= balance) :then\n   balance := balance - amount\n :else\n   console.print(\"The balance is less than the amount\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Execution of the <code>if:then:else<\/code> takes place as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The expression <code>(amount &lt;= balance)<\/code>, the statement <code>balance := balance - amount<\/code>, and the statement <code>console.print(\"The balance is less than the amount\")<\/code> are the actual parameters (arguments) of the invocation of<code> if:then:else<\/code>.<\/li>\n\n\n\n<li>The boolean expression <code>amount &lt;= balance<\/code> is evaluated and assigned to the parameter <code>cond<\/code>.<\/li>\n\n\n\n<li>The statement <code>balance := balance - amount<\/code> is a further binding<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>of the virtual <code>thenPart<\/code>.<\/li>\n\n\n\n<li>The statement <code>console.print(\"...\")<\/code> is a further binding of the <code>elsePart<\/code>.<\/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=7470\" 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 conditional statement is a statement that execute different statements depending on the value of a boolean expression. If:then The if:then statement used in previous sections is an example of a conditional statement. The code below shows a sketch of a definition of if:then as a control method. As mentioned, if:then is a built-in primitive [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3671,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7470","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\/7470","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=7470"}],"version-history":[{"count":13,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7470\/revisions"}],"predecessor-version":[{"id":9607,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7470\/revisions\/9607"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3671"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}