{"id":278,"date":"2023-02-13T10:03:25","date_gmt":"2023-02-13T10:03:25","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=278"},"modified":"2024-07-08T13:10:39","modified_gmt":"2024-07-08T12:10:39","slug":"conditional-statements","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-1\/conditional-statements\/","title":{"rendered":"Conditional statements"},"content":{"rendered":"\n<p>Conditional statements are common among programming languages and they are used to perform actions or calculations based on whether a condition is evaluated as true or false.<\/p>\n\n\n\n<p>If-then-else statements or conditional expressions are essential features of programming languages and they make programs more useful to users. In Python, conditional statements will end with a colon: and the section of code to be run will be indented further from the left of the screen that the conditional statement line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">If Statements<\/h2>\n\n\n\n<p>An if statement only runs a section of code&nbsp;<strong>if&nbsp;<\/strong>the condition in the statement results in a boolean True.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Program starts\")\n\ntotal_value = 99\nminimum_value = 40\n\nif total_value &gt; minimum_value:\n    print(\"Totals\")\n    print(total_value)\n\nprint(\"Program ends\")<\/code><\/pre>\n\n\n\n<p>If we run this we will see a line saying Total and then the 99 store in total_value is printed beneath it.<\/p>\n\n\n\n<p>If we change total_value to something less than or equal to 40 then the comparison is no longer True. Because it isn\u2019t true the two indented print lines are no longer executed.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">If Else Statements<\/h2>\n\n\n\n<p>An if else statement runs a section of code&nbsp;<strong>if&nbsp;<\/strong>the condition in the statement results in a boolean&nbsp;<strong>True&nbsp;<\/strong>but has a second section of code for when the condition is&nbsp;<strong>False<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Program starts\")\n\ntotal_value = 35\nminimum_value = 40\n\nif total_value &gt; minimum_value:\n  print(\"Totals\")\n  print(total_value)\nelse:\n  print(\"Total still too small\")\n\nprint(\"Program ends\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">If ElseIf Else Statements<\/h2>\n\n\n\n<p>An if, elseif, else statement runs a section of code&nbsp;<strong>if&nbsp;<\/strong>the condition in the statement results in a boolean True but if it is False then it checks a subsequent if statement but it also has a section of code for when none of the if statements return True.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Program starts\")\n\ntotal_value = 40\nminimum_value = 40\n\nif total_value &gt; minimum_value:\n  print(\"Totals\")\n  print(total_value)\nelif total_value == minimum_value:\n  print(\"Just made it\")\nelse:\n  print(\"Total still too small\")\n\nprint(\"Program ends\")<\/code><\/pre>\n\n\n\n<p>elif stands for else if and must have a comparison that returns True or False after it.<\/p>\n\n\n\n<p>You can use multiple elif statements to carry out multiple if statements in which you stop checking once a match is found.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Program starts\")\n\ntotal_value = 38\nminimum_value = 40\n\nif total_value &gt; minimum_value:\n  print(\"Totals\")\n  print(total_value)\nelif total_value == minimum_value:\n  print(\"Just made it\")\nelif total_value &gt; (minimum_value - 5):\n  print(\"Just too small\")\nelse:\n  print(\"Total still too small\")\n\nprint(\"Program ends\")<\/code><\/pre>\n\n\n\n<p>In this code we first check if the total is above the minimum value. If it isn\u2019t we then check if it is equal to the minimum value. If it doesn\u2019t match that then we check if it is greater than 5 below the minimum value. And finally, if nothing else has been matched we use the else statement to catch every value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p><strong>Indentation matters!<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if x &gt; y:\n  z = True\n  if a &gt; b:\n    total = a\n  else:\n    z = False\nprint(z)<\/code><\/pre>\n\n\n\n<p>In most programming languages you will see curly brackets or braces {} used to surround sections of code. The whitespace and indentation from the left of the screen is just to make it more readable.<\/p>\n\n\n\n<p>In Python, indentation is&nbsp;<strong>everything<\/strong>. The indentation controls what is considered a block of code. Every line beginning the same distance from the left of the screen is seen as a block of code.<\/p>\n\n\n\n<p>Use brackets for long expressions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(really_long_variable_name &gt; another_long_variable_name and really_long_variable_name &lt; yet_another_long_variable_name):<\/code><\/pre>\n\n\n\n<p>If you have an expression that takes up a lot of space or wraps onto another line you can use brackets around it.<\/p>\n\n\n\n<p>And remember to use your comments to explain not just what the file is doing, but throughout the code to explain what each is there for. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Pass<\/h2>\n\n\n\n<p>During development you sometimes need to enter some code into a code block but haven&#8217;t yet designed that part. Just entering a comment isn&#8217;t enough to count as a valid block of code. This is where you can use the <strong>pass<\/strong> keyword. It is a placeholder that you will replace later on in development.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if x &gt;= y:\n    # Add some code here in the future \n    pass\nelse:\n    print(\"x is too small\")<\/code><\/pre>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-1\/challenge-2\/\" data-type=\"page\" data-id=\"294\">Next: Challenge 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conditional statements are common among programming languages and they are used to perform actions or calculations based on whether a condition is evaluated as true or false. If-then-else statements or conditional expressions are essential features of programming languages and they make programs more useful to users. In Python, conditional statements will end with a colon:&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-1\/conditional-statements\/\">Continue reading <span class=\"screen-reader-text\">Conditional statements<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":14,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-278","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":7,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/278\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/278\/revisions\/1386"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/14"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}