{"id":261,"date":"2022-05-16T11:20:39","date_gmt":"2022-05-16T10:20:39","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/?page_id=261"},"modified":"2024-11-28T14:53:40","modified_gmt":"2024-11-28T14:53:40","slug":"conditional-statements","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/conditional-statements\/","title":{"rendered":"Conditional Statements"},"content":{"rendered":"<h2 style=\"color: red\">Selection<\/h2>\n<p>Computer programs require to make decisions and execute different sets of instructions depending on the value variables.\u00a0 For example, a program will take a different action if an exam mark is a pass than if the mark is a fail.<\/p>\n<h2 style=\"color: red\">Selection Operators<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-264\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16113034\/Screenshot-2022-05-16-102913-300x144.png\" alt=\"\" width=\"573\" height=\"275\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16113034\/Screenshot-2022-05-16-102913-300x144.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16113034\/Screenshot-2022-05-16-102913-624x299.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16113034\/Screenshot-2022-05-16-102913.png 684w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/p>\n<h2 style=\"color: red\">Conditional Statements (IF Statements)<\/h2>\n<p data-reactid=\".gdj0p6bho.0.0.0.1:0.1.0.$0.$1.$0\"><span data-reactid=\".gdj0p6bho.0.0.0.1:0.1.0.$0.$1.$0.0\">Allowing programs to make decisions is an important part of programming. \u2018If statements\u2019 are used to let a program select an action depending upon the value of variables held in the program.<\/span><\/p>\n<p data-reactid=\".gdj0p6bho.0.0.0.1:0.1.0.$0.$1.$1\"><span data-reactid=\".gdj0p6bho.0.0.0.1:0.1.0.$0.$1.$1.0\">Here is an example of how an if statement can be used to select what happens next in a section of code designed to:<\/span><\/p>\n<ol>\n<li data-reactid=\".gdj0p6bho.0.0.0.1:0.1.0.$0.$1.$1\">Check if the user is old enough to vote<\/li>\n<li data-reactid=\".gdj0p6bho.0.0.0.1:0.1.0.$0.$1.$1\">Check if the user qualifies for a discount<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-267\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114122\/Screenshot-2022-05-16-114100-300x57.png\" alt=\"\" width=\"747\" height=\"142\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114122\/Screenshot-2022-05-16-114100-300x57.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114122\/Screenshot-2022-05-16-114100-1024x193.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114122\/Screenshot-2022-05-16-114100-768x145.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114122\/Screenshot-2022-05-16-114100-624x118.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114122\/Screenshot-2022-05-16-114100.png 1260w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-269\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114250\/Screenshot-2022-05-16-114226-300x63.png\" alt=\"\" width=\"676\" height=\"142\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114250\/Screenshot-2022-05-16-114226-300x63.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114250\/Screenshot-2022-05-16-114226-1024x215.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114250\/Screenshot-2022-05-16-114226-768x162.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114250\/Screenshot-2022-05-16-114226-624x131.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16114250\/Screenshot-2022-05-16-114226.png 1260w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/p>\n<h2 style=\"color: red\">Complex Conditional Statements (IF Statements)<\/h2>\n<p>A complex condition depends upon two or more conditional statements being true or false.\u00a0 Programming languages have <strong>logical operators<\/strong>\u00a0 &#8211;\u00a0 \u00a0<strong>AND<\/strong>, <strong>OR, NOT <\/strong>&#8211; that can be used to implement complex conditions in program instruction.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-271\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121233\/Screenshot-2022-05-16-120456-300x130.png\" alt=\"\" width=\"734\" height=\"318\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121233\/Screenshot-2022-05-16-120456-300x130.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121233\/Screenshot-2022-05-16-120456-1024x442.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121233\/Screenshot-2022-05-16-120456-768x332.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121233\/Screenshot-2022-05-16-120456-624x269.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121233\/Screenshot-2022-05-16-120456.png 1260w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-274\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121755\/Screenshot-2022-05-16-121637-300x48.png\" alt=\"\" width=\"781\" height=\"125\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121755\/Screenshot-2022-05-16-121637-300x48.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121755\/Screenshot-2022-05-16-121637-1024x165.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121755\/Screenshot-2022-05-16-121637-768x124.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121755\/Screenshot-2022-05-16-121637-624x101.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121755\/Screenshot-2022-05-16-121637.png 1260w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-275\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121815\/Screenshot-2022-05-16-121717-300x87.png\" alt=\"\" width=\"817\" height=\"237\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121815\/Screenshot-2022-05-16-121717-300x87.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121815\/Screenshot-2022-05-16-121717-1024x297.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121815\/Screenshot-2022-05-16-121717-768x223.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121815\/Screenshot-2022-05-16-121717-624x181.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16121815\/Screenshot-2022-05-16-121717.png 1097w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/p>\n<h2 style=\"color: red\">IF-ELIF Statements<\/h2>\n<p><span style=\"color: #ff0000\">Using <strong>multiply IF statements <\/strong>would mean that the program will check every IF statement condition.\u00a0 Even if the first condition is met, the program will still check all conditions.\u00a0 This will t<strong>ake up more processing time that is not required<\/strong>.\u00a0<\/span> To overcome this, we can use an <strong>IF-ElIF<\/strong>\u00a0statement.\u00a0 With an <strong>elif<\/strong> statement &#8211; once a condition has been met, the program will move on.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-315\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16160119\/Screenshot-2022-05-16-160046-300x165.png\" alt=\"\" width=\"802\" height=\"441\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16160119\/Screenshot-2022-05-16-160046-300x165.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16160119\/Screenshot-2022-05-16-160046-1024x562.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16160119\/Screenshot-2022-05-16-160046-768x422.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16160119\/Screenshot-2022-05-16-160046-624x343.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16160119\/Screenshot-2022-05-16-160046.png 1260w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/p>\n<h2 style=\"color: red\">Structure Diagram \u2013 If Statement Example<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-288\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16143724\/Screenshot-2022-05-16-143540-262x300.png\" alt=\"\" width=\"767\" height=\"878\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16143724\/Screenshot-2022-05-16-143540-262x300.png 262w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16143724\/Screenshot-2022-05-16-143540-896x1024.png 896w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16143724\/Screenshot-2022-05-16-143540-768x878.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16143724\/Screenshot-2022-05-16-143540-624x713.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16143724\/Screenshot-2022-05-16-143540.png 1102w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selection Computer programs require to make decisions and execute different sets of instructions depending on the value variables.\u00a0 For example, a program will take a different action if an exam mark is a pass than if the mark is a fail. Selection Operators Conditional Statements (IF Statements) Allowing programs to make decisions is an important [&hellip;]<\/p>\n","protected":false},"author":79984,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-261","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/users\/79984"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":18,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages\/261\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages\/261\/revisions\/608"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/media?parent=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}