
{"id":406,"date":"2018-10-02T16:10:03","date_gmt":"2018-10-02T16:10:03","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=406"},"modified":"2018-10-02T16:10:03","modified_gmt":"2018-10-02T16:10:03","slug":"if-else-if-else-statement-glossary-entry","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/if-else-if-else-statement-glossary-entry\/","title":{"rendered":"if-else if-else statement (Glossary Entry)"},"content":{"rendered":"<p>Conditional statements are what add the decision making and to some extent, business logic to software. At a fundamental level, a computer does only a few distinct tasks:<\/p>\n<ul>\n<li>Fetch and store<\/li>\n<li>Modify (arithmetic and bitwise operations)<\/li>\n<li>Flow control (conditionally branch)<\/li>\n<\/ul>\n<p>Like the while and do-while statements, the if-else if-else also performs conditional tests, but in contrast, they do not perform iteration (looping).<\/p>\n<p><b>Single IF statement<\/b><\/p>\n<p>The simplest for is the single <b>if<\/b> statement. This construct simply executes an additional block of code when a condition is found to be <b>true<\/b> (non zero in C).<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">if ( &lt;conditional statement&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process\r\n\r\n}\r\n<\/pre>\n<p>This is depicted in the following flow-chart.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-407\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIF.png\" alt=\"\" width=\"450\" height=\"445\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIF.png 652w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIF-300x297.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>Note how the consequent branch re-joins the main program flow.<\/p>\n<p><b>if-else<\/b><\/p>\n<p>A Boolean condition is either TRUE or FALSE. You use the combination of <b>if <\/b>and <b>else<\/b> statements to perform different processes for each outcome. This construct simply executes one block of code for the TRUE condition, and another block of code for the FALSE.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true\">if ( &lt;conditional statement&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for TRUE\r\n\r\n} else {\r\n\r\n\/\/Consequent Process for FALSE\r\n\r\n}<\/pre>\n<p>This is depicted in the following flow-chart.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-408\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSE.png\" alt=\"\" width=\"600\" height=\"291\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSE.png 993w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSE-300x145.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSE-768x372.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Once again, the code rejoins the main flow.<\/p>\n<p><b>if-else if-else<\/b><\/p>\n<p>This is used to test multiple conditions <b>using a process of elimination<\/b>. As with the previous two cases, the outcome always results with one block of code (consequent process) being <b>exclusively<\/b> executed.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true\">if ( &lt;conditional statement 1&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for Conditional Statement 1\r\n\r\n} else if ( &lt;conditional statement 2&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for Conditional Statement 2\r\n\r\n} else if ( &lt;conditional statement N&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for Conditional Statement N\r\n\r\n} else {\r\n\r\n\/\/Consequent Process for all other conditions\r\n\r\n\/\/(optional)\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>This is depicted in the following flow-chart.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-409\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSEIFELSE.png\" alt=\"\" width=\"1120\" height=\"1197\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSEIFELSE.png 1120w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSEIFELSE-281x300.png 281w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSEIFELSE-768x821.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSEIFELSE-958x1024.png 958w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartIFELSEIFELSE-935x999.png 935w\" sizes=\"auto, (max-width: 1120px) 100vw, 1120px\" \/><\/p>\n<p>Note again that <b>each consequent process is mutually exclusive<\/b> and evaluated in a specific sequence. For example, if both conditional statements A and B happen to be TRUE, only consequent process A would execute as this is tested first.<\/p>\n<p><i>Contrast this with multiple if-else statements<\/i><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">if ( &lt;conditional statement 1&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for Conditional Statement 1\r\n\r\n} else {\r\n\r\n\/\/Complimentary Consequent Process\u00a0\r\n\r\n}\r\n\r\nif ( &lt;conditional statement 2&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for Conditional Statement 2\r\n\r\n} else {\r\n\r\n\/\/Complimentary Consequent Process\u00a0\r\n\r\n}\r\n\r\nif ( &lt;conditional statement N&gt;)\u00a0 {\r\n\r\n\/\/Consequent Process for Conditional Statement N\r\n\r\n} else {\r\n\r\n\/\/Complimentary Consequent Process\u00a0\r\n\r\n}<\/pre>\n<p>This is depicted in the following flow-chart.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-410\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartMultipleIFELSE.png\" alt=\"\" width=\"400\" height=\"1013\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartMultipleIFELSE.png 531w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartMultipleIFELSE-118x300.png 118w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartMultipleIFELSE-404x1024.png 404w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/FlowChartMultipleIFELSE-394x999.png 394w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><b>Note the key difference here<\/b>. ALL conditions are tested in order. For example, if conditional statements A and C are both true, then consequent processes A and B will <strong>both<\/strong> be executed (in sequence).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conditional statements are what add the decision making and to some extent, business logic to software. At a fundamental level, a computer does only a few distinct tasks: Fetch and store Modify (arithmetic and bitwise operations) Flow control (conditionally branch) Like the while and do-while statements, the if-else if-else also performs conditional tests, but in&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/if-else-if-else-statement-glossary-entry\/\">Continue reading <span class=\"screen-reader-text\">if-else if-else statement (Glossary Entry)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":153,"menu_order":60,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-406","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":1,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/406\/revisions"}],"predecessor-version":[{"id":411,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/406\/revisions\/411"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/153"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}