{"id":4805,"date":"2020-01-30T17:50:00","date_gmt":"2020-01-30T15:50:00","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=4805"},"modified":"2025-09-04T17:29:20","modified_gmt":"2025-09-04T15:29:20","slug":"conditional-statements","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/","title":{"rendered":"Conditional statements"},"content":{"rendered":"<div class=\"su-box su-box-style-soft\" id=\"\" style=\"border-color:#5c0808;border-radius:7px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#8f3b3b;color:#FFFFFF;border-top-left-radius:5px;border-top-right-radius:5px\">Please note!<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:5px;border-bottom-right-radius:5px\">This article is under heavy development, has parts of it missing and may include mistakes!<\/div><\/div>\n<p><strong>This is the comprehensive and overly long page about writing a basic conditional statement.<\/strong><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Introduction\" >Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#The_if_statement\" >The if statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Relational_operators\" >Relational operators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#The_if-else_statement\" >The if-else statement<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Handling_empty_if_statement_code_blocks\" >Handling empty if statement code blocks<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Nested_conditionals\" >Nested conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Using_if-else_if-else\" >Using if-else if-else<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Logical_operators\" >Logical operators<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Truth_tables\" >Truth tables<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#The_switch_statement\" >The switch statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Some_tips_and_tricks\" >Some tips and tricks<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Using_only_variable_name_as_expression\" >Using only variable name as expression<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Single-line_conditionals_without_braces\" >Single-line conditionals without braces<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Problems_with_single-line_conditionals\" >Problems with single-line conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#The_dangling_else_problem\" >The dangling else problem<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Refactoring_bad_nested_conditional_statements\" >Refactoring bad nested conditional statements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Function_calls_in_conditionals\" >Function calls in conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Assignments_in_conditionals\" >Assignments in conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#Ternary_operator\" >Ternary operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/#De_Morgans_law\" >De Morgan&#8217;s law<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Conditional statements are the most basic way of managing program&#8217;s control flow. In a more literal sense, it enables altering which actions the program takes, depending on the conditions that we check against.<\/p>\n<p>For example consider an event ticketing system, that checks for the person&#8217;s age and conditionally chooses an appropriate action. Minors are not allowed to purchase a ticket, elderly will have a reduced price for the ticket and everyone else will pay the full price.<\/p>\n<p>In any conditional statement, we evaluate an expression that can only have two possible results &#8211; <strong>true<\/strong> or\u00a0<strong>false<\/strong>. In different programming languages they take different forms. In the C89\/C90 standard, conditions are evaluated using: <strong>true is 1<\/strong> and <strong>false is 0<\/strong>. In the C99 standard, the keywords true and false can be used by including a library <span class=\"lang:c decode:true crayon-inline\">stdbool.h<\/span> , which contains them. From C23 standard, they are a core part of the language.<\/p>\n<p>Note, that in many of the following sections, there are also <strong><span style=\"color: #ff0000;\">negative examples &#8211; what not to do<\/span><\/strong>. These will be highlighted from the rest of the text.<\/p>\n<h2 data-select-like-a-boss=\"1\"><span class=\"ez-toc-section\" id=\"The_if_statement\"><\/span>The if statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The if statement has the following basic structure<br \/>\n<span class=\"lang:c decode:true crayon-inline\">if ( expression ) statement<\/span><\/p>\n<p>Where the expression is the condition to be evaluated and statement refers to all statements that run only if the expression is evaluated to true.<\/p>\n<p><strong>The if statement is evaluated to true when the expression evaluates to any non-zero value!<\/strong> Note, that while 1 is commonly used to represent true, values like -5, 100, or any other non-zero number will also be treated as true. (See Tips and Tricks for more details.)<\/p>\n<p>Let&#8217;s look at an example. In this case, we have initialized the variable age to 10. The conditional statement checks if the age is less than 18. Since\u00a0 it is, everything in the code block marked by the braces will be executed.<\/p>\n<pre class=\"lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    int age = 10;\r\n    if (age &lt; 18)\r\n    {\r\n        printf(\"You are underage!\\n\");\r\n        printf(\"You are not allowed to the event without parenthal supervision!\\n\");\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p><strong>Try it out<\/strong><\/p>\n<ul>\n<li>Compile and run the code, see what happens<\/li>\n<li>Change the age to 20, will the printf() statements be executed now?<\/li>\n<li>Try setting the age to 18 and try again. This is what is known as an <strong>edge case<\/strong>. Edge cases are boundary values and must always be tested, as they are common places for mistakes to creep in!<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Relational_operators\"><\/span>Relational operators<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Assume the following declarations and values for the examples:<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">int a = 10;\r\nint b = 20;\r\nint c = 20;\r\n<\/pre>\n<p>Note: The result of relational operators in C is always either integer value 1 (true) or 0 (false).<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 119px;\">\n<tbody>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\"><strong>Operator<\/strong><\/td>\n<td style=\"width: 60.9704%; height: 17px;\"><strong>Description<\/strong><\/td>\n<td style=\"width: 24.6835%; height: 17px;\"><strong>Example<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\">==<\/td>\n<td style=\"width: 60.9704%; height: 17px;\">Evaluates to true when both operands are equal<\/td>\n<td style=\"width: 24.6835%; height: 17px;\">(a == c) &#8211;&gt; false<br \/>\n(b == c) &#8211;&gt; true<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\">!=<\/td>\n<td style=\"width: 60.9704%; height: 17px;\">Evaluates to true when operands are not equal. ! (exclamation symbol is used for negation)<\/td>\n<td style=\"width: 24.6835%; height: 17px;\">(a != c) &#8211;&gt; true<br \/>\n(b != c) &#8211;&gt; false<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\">&lt;<\/td>\n<td style=\"width: 60.9704%; height: 17px;\">Evaluates to true if left operand is smaller than the right operand<\/td>\n<td style=\"width: 24.6835%; height: 17px;\">(a &lt; c) &#8211;&gt; true<br \/>\n(b &lt; c) &#8211;&gt; false<br \/>\n(c &lt; a) &#8211;&gt; false<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\">&lt;=<\/td>\n<td style=\"width: 60.9704%; height: 17px;\">Evaluates to true if left operand is smaller than or equal to the right operand<\/td>\n<td style=\"width: 24.6835%; height: 17px;\">(a &lt;= c) &#8211;&gt; true<br \/>\n(b &lt;= c) &#8211;&gt; true<br \/>\n(c &lt;= a) &#8211;&gt; false<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\">&gt;<\/td>\n<td style=\"width: 60.9704%; height: 17px;\">Evaluates to true if left operand is greater than the right operand<\/td>\n<td style=\"width: 24.6835%; height: 17px;\">(a &gt; c) &#8211;&gt; false<br \/>\n(b &gt; c) &#8211;&gt; false<br \/>\n(c &gt; a) &#8211;&gt; true<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 14.346%; height: 17px;\">&gt;=<\/td>\n<td style=\"width: 60.9704%; height: 17px;\">Evaluates to true if left operand is greater\u00a0 than or equal to the right operand<\/td>\n<td style=\"width: 24.6835%; height: 17px;\">(a &gt;= c) &#8211;&gt; false<br \/>\n(b &gt;= c) &#8211;&gt; true<br \/>\n(c &gt;= a) &#8211;&gt; true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"The_if-else_statement\"><\/span>The if-else statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To define an alternative action when the condition is not true, we can use the else keyword. The statements inside the else code block will only run when the expression is evaluates to false.<\/p>\n<p><span class=\"lang:c decode:true crayon-inline\">if ( expression ) statement else statement<\/span><\/p>\n<p>Let&#8217;s build on the previous example by adding an else statement. Now depending on the value of the age variable, either the statements in the if code block run or the ones in the else code block.<\/p>\n<pre class=\"lang:c decode:true\">int age = 30;\r\n\r\nif (age &lt; 18)\r\n{\r\n    printf(\"You are underage!\\n\");\r\n    printf(\"You are not allowed to the event without parental supervision!\\n\");\r\n}\r\nelse\r\n{\r\n    printf(\"Age is verified, you are welcome to participate!\\n\");\r\n}<\/pre>\n<p><strong>Try it out!<\/strong><\/p>\n<p>The initialized value for the age variable is set to 30.\u00a0 The code will:<br \/>\n1. Evaluate the condition <span class=\"lang:c highlight:0 decode:true crayon-inline \">if (age &lt; 18)<\/span> . This will be false, as age is set to 30.<br \/>\n2. The if statement&#8217;s code block (lines 4 &#8211; 7) will be skipped.<br \/>\n3. The program continues to the the else keyword on line 8 and executes the contents in the braces.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Never leave an empty code block!<\/span> This is considered bad style and leads to confusion! If you don&#8217;t need any code to run when the condition is true, invert the condition.\u00a0<\/strong><\/p>\n<pre class=\"theme:undefined toolbar:2 lang:c decode:true\">if (age &lt; 18) \/\/ DO NOT DO THIS!!! This is incorrect!\r\n{\r\n\r\n}\r\nelse\r\n{\r\n    printf(\"Age is verified, you are welcome to participate!\\n\");\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Handling_empty_if_statement_code_blocks\"><\/span>Handling empty if statement code blocks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You should never leave an empty code block. If it happens, there are are two possible options how to solve this.<\/p>\n<ol>\n<li>Find the opposite condition and use that<\/li>\n<li>Invert the\u00a0 original condition using the ! (not) operator<\/li>\n<\/ol>\n<p>Let&#8217;s take the bad example from the previous section and fix it. First let&#8217;s try to find an opposite condition. To find people who are <strong>not<\/strong> younger than 18, we have to look for people who are over 18 years old\u00a0<strong>or exactly 18\u00a0<\/strong> years old.<\/p>\n<p><strong>Common mistake:<\/strong> The opposite of <span class=\"lang:c decode:true crayon-inline \">(age &lt; 18)<\/span>\u00a0 is not <span class=\"lang:c decode:true crayon-inline \">age &gt; 18<\/span> , but rather <span class=\"lang:c decode:true crayon-inline \">(age &gt;= 18)<\/span> . Watch out for edge cases!<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">if (age &gt;= 18)\r\n{\r\n    printf(\"Age is verified, you are welcome to participate!\\n\");\r\n}<\/pre>\n<p>Alternatively, you can invert the condition by using the inversion operator (!). Take the original condition, wrap it in parenthesis and place ! in front.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">if (!(age &lt; 18))\r\n{\r\n    printf(\"Age is verified, you are welcome to participate!\\n\");\r\n}<\/pre>\n<h2 data-select-like-a-boss=\"1\"><span class=\"ez-toc-section\" id=\"Nested_conditionals\"><\/span>Nested conditionals<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Multiple if statements can be nested inside of each other. Be careful with this however, as it can mess up the program structure to the extent where the program becomes unmaintainable. Always consider using <strong>else if<\/strong> statements, combining conditionals using <strong>logical operators<\/strong> and other <strong>refactoring techniques<\/strong> before writing multiple levels of conditionals. Compare the following code to the one presented in the next chapter.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true \">if (age &gt;= 18)\r\n{\r\n    if (age &lt; 65)\r\n    {\r\n        printf(\"Tickets for adults: 10 euros\\n\");\r\n    }\r\n    else\r\n    {\r\n        printf(\"Tickets for seniors: 4 euros\\n\");\r\n    }\r\n}\r\nelse\r\n{\r\n    printf(\"Minors only allowed with parental supervision\\n\");\r\n}\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Using_if-else_if-else\"><\/span>Using if-else if-else<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To test against multiple related conditions, you can use else if statements. It tests all the conditions in order, starting from the top.\u00a0As soon as one of the conditions evaluates to true, the corresponding code block is executed. No following conditions are checked once one of them evaluates to true!<\/p>\n<p>If no conditional statements are true, an else statement will be executed, if it exists.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">int age = 30;\r\nif (age &lt; 18)\r\n{\r\n    printf(\"Minors only allowed with parental supervision\\n\");\r\n}\r\nelse if (age &gt;= 65)\r\n{\r\n    printf(\"Tickets for seniors: 4 euros\\n\");\r\n}\r\nelse\r\n{\r\n    printf(\"Tickets for adults: 10 euros\\n\");\r\n}\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Logical_operators\"><\/span>Logical operators<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Logical operators allow joining of multiple conditional statements logically into one.<\/p>\n<p>There are a total of three logical operators defined in the C language.\u00a0 Note, that logical operators are different from bitwise operators.<\/p>\n<p>Assume the following declarations and values for the examples:<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">int a = 0;\r\nint b = 10;\r\nint c = 20;\r\n<\/pre>\n<table style=\"border-collapse: collapse; width: 100%; height: 262px;\">\n<tbody>\n<tr style=\"height: 16px;\">\n<td style=\"width: 15.3629%; height: 16px;\"><strong>Operator<\/strong><\/td>\n<td style=\"width: 22.551%; height: 16px;\"><strong>Operation<\/strong><\/td>\n<td style=\"width: 29.4574%; height: 16px;\"><strong>Result is true, if &#8230;<\/strong><\/td>\n<td style=\"width: 32.6286%;\"><strong>Example<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 82px;\">\n<td style=\"width: 15.3629%; text-align: center; height: 82px;\" width=\"208\">&amp;&amp;<\/td>\n<td style=\"width: 22.551%; height: 82px;\" width=\"426\">Logical AND<\/p>\n<p>Conjunction<\/td>\n<td style=\"width: 29.4574%; height: 82px;\" width=\"471\">\u2026 both sides are true<\/td>\n<td style=\"width: 32.6286%;\">(a\u00a0 &gt; 0 &amp;&amp; b &gt; 0) &#8211;&gt; false<br \/>\n(b &gt; 0 &amp;&amp; c &gt; 0) &#8211;&gt; true<br \/>\n((a &lt; b) &amp;&amp; (b &lt; c)) &#8211;&gt; true<\/td>\n<\/tr>\n<tr style=\"height: 82px;\">\n<td style=\"width: 15.3629%; text-align: center; height: 82px;\" width=\"208\">||<\/td>\n<td style=\"width: 22.551%; height: 82px;\" width=\"426\">Logical OR<\/p>\n<p>Disjunction<\/td>\n<td style=\"width: 29.4574%; height: 82px;\" width=\"471\">&#8230; at least one side is true<\/td>\n<td style=\"width: 32.6286%;\">(a &gt; 0 ||\u00a0 b &gt; 0) &#8211;&gt; true<br \/>\n((a &gt; b) || (b &gt; c)) &#8211;&gt; false<br \/>\n((a &lt; b) || (b &gt; c)) &#8211;&gt; true<\/td>\n<\/tr>\n<tr style=\"height: 82px;\">\n<td style=\"width: 15.3629%; text-align: center; height: 82px;\" width=\"208\">!<\/td>\n<td style=\"width: 22.551%; height: 82px;\" width=\"426\">Logical negation<\/p>\n<p>Inversion<\/td>\n<td style=\"width: 29.4574%; height: 82px;\" width=\"471\">&#8230; initial condition is false<\/td>\n<td style=\"width: 32.6286%;\">(!a) &#8211;&gt; true<br \/>\n(!b) &#8211;&gt; false<br \/>\n(!a || !b) &#8211;&gt; true<br \/>\n(!(a || b)) &#8211;&gt; false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><span class=\"ez-toc-section\" id=\"Truth_tables\"><\/span>Truth tables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"su-row\"><div class=\"su-column su-column-size-1-3\"><div class=\"su-column-inner su-u-clearfix su-u-trim\">\n<p>Logical AND<\/p>\n<table style=\"border-collapse: collapse; width: 56.162%; height: 81px;\">\n<tbody>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">A<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">B<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">A &amp;&amp; B<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 7.69227%; height: 17px; text-align: center;\">0<\/td>\n<td style=\"width: 5.82745%; height: 17px; text-align: center;\">0<\/td>\n<td style=\"width: 42.9001%; height: 17px; text-align: center;\">0<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">0<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">0<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">0<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">0<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div><\/div>\n<div class=\"su-column su-column-size-1-3\"><div class=\"su-column-inner su-u-clearfix su-u-trim\">\n<p>Logical OR<\/p>\n<table style=\"border-collapse: collapse; width: 56.162%; height: 81px;\">\n<tbody>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">A<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">B<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">A || B<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 7.69227%; height: 17px; text-align: center;\">0<\/td>\n<td style=\"width: 5.82745%; height: 17px; text-align: center;\">0<\/td>\n<td style=\"width: 42.9001%; height: 17px; text-align: center;\">0<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">0<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">1<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">0<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">1<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 5.82745%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 42.9001%; height: 16px; text-align: center;\">1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div><\/div><div class=\"su-column su-column-size-1-3\"><div class=\"su-column-inner su-u-clearfix su-u-trim\">\n<p>Logical inversion<\/p>\n<table style=\"border-collapse: collapse; width: 27.1127%; height: 81px;\">\n<tbody>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">A<\/td>\n<td style=\"width: 13.9527%; height: 16px; text-align: center;\">!A<\/td>\n<\/tr>\n<tr style=\"height: 17px;\">\n<td style=\"width: 7.69227%; height: 17px; text-align: center;\">0<\/td>\n<td style=\"width: 13.9527%; height: 17px; text-align: center;\">1<\/td>\n<\/tr>\n<tr style=\"height: 16px;\">\n<td style=\"width: 7.69227%; height: 16px; text-align: center;\">1<\/td>\n<td style=\"width: 13.9527%; height: 16px; text-align: center;\">0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div><\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"The_switch_statement\"><\/span>The switch statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The switch statement will attempt to find an exact match between the expressions.\u00a0 If an expression is matched, it will run either until the <strong>end of the switch<\/strong> statement or a <strong>break<\/strong> statement. Note that if no break statement is encountered, the statements in the later cases will also be executed. This can either be a bug or wanted behavior, depending on the use case.<\/p>\n<p>The default case is optional, but highly recommended. If no cases match the expression, the statements in the default case will be executed.<\/p>\n<pre class=\"toolbar:2 nums-toggle:false lang:c decode:true\">switch (expression)\r\n{\r\n    case constant or expression:\r\n        statements\r\n        break;\r\n    case constant or expression:\r\n        statements\r\n        break;\r\n    default:\r\n        statements\r\n}<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Some_tips_and_tricks\"><\/span>Some tips and tricks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The following section is a combination of various ways to use conditional statements efficiently and tricks that you might encounter, but are not actually recommended.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Using_only_variable_name_as_expression\"><\/span>Using only variable name as expression<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In C, the integer 0 evaluates to\u00a0 <span class=\"lang:c decode:true crayon-inline\">false<\/span> . All other non-zero values are considered <span class=\"lang:c decode:true crayon-inline\">true<\/span>.<\/p>\n<p>This allows us to either use the variable name or the inversion of it to test for error codes, invalid values etc quickly.<\/p>\n<p>Try the following code with the initialized value to 0, 1, -1 etc.<\/p>\n<pre class=\"toolbar:2 nums-toggle:false lang:c decode:true \">int isInputDataValid = 0;\r\n\r\nif (!isInputDataValid) \/\/ any non-zero value will evaluate to true (notice inversion)\r\n{\r\n    printf(\"The data validation has failed!\\n\");\r\n}\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Single-line_conditionals_without_braces\"><\/span>Single-line conditionals without braces<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the previous examples, we have shown the statements to be executed are always within braces.\u00a0 This is not a requirement. In C, both loops and conditional statements can be written without any braces, however in that case, <strong>only the following statement\u00a0<\/strong>will be executed conditionally as a part of it.<\/p>\n<p>The following example is a perfectly valid if-else statement in C and acts as expected.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">int age = 30;\r\nif (age &lt; 18)\r\n    printf(\"You are underage!\\n\");\r\nelse\r\n    printf(\"Age is verified, you are welcome to participate!\\n\");<\/pre>\n<p>These are often preferred to save space and are frequently used by experienced programmers. Sometimes you will also see that the statement will be on the same line as the conditional statement starts to save even more space.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">int age;\r\n\r\nprintf(\"How old are you?\\n\");\r\nscanf(\"%d\", &amp;age);\r\n\r\nif (age &lt; 18) exit(1);\r\n\r\nprintf(\"Welcome to the event!\\n\");<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Problems_with_single-line_conditionals\"><\/span>Problems with single-line conditionals<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Less experienced should however be careful when using such compact notions. There are multiple reasons to avoid it when starting to program.<\/p>\n<p><strong>1. Less consistent style<\/strong><\/p>\n<p>You will have to differentiate and notice various styles of writing the statements. This might be confusing in the beginning. Sometimes you will miss a statement here or there and it will cause bugs that are annoying to locate.<\/p>\n<p><strong>2. More prone to accidental mistakes when statements are added or removed.<\/strong><\/p>\n<p>You will often make alterations to your code during writing it or later on. Without enough experience you might not remember to add the braces when you add a line.<\/p>\n<p>Let&#8217;s take the previous age check where it only notified about being underage and add another <span class=\"lang:c decode:true crayon-inline \">printf()<\/span>\u00a0 statement that reminds about requiring parental supervision. We&#8217;ll also indent it nicely so it looks like it might be a part of the conditional statement.<\/p>\n<pre class=\"theme:github-copy toolbar:2 nums:false lang:c decode:true \">if (age &lt; 18)\r\n    printf(\"You are underage!\\n\");\r\n    printf(\"You are not allowed to the event without parental supervision!\\n\");\r\n\r\nprintf(\"The tickets cost 20 euros!\"\\n);<\/pre>\n<p>In this case when the age is less than 18, all 3 lines will be printed out, which is to be expected. When you forget to test the other case &#8211; where the age is 18 or greater, you will not even know that there is a mistake.\u00a0 For those 18 and over, it will still now warn about requiring parental supervision, even though that was not the intent of the developer.<\/p>\n<p><strong>3. Avoid ambiguity when nested conditionals are used.\u00a0\u00a0<\/strong><\/p>\n<p>This is also called the dangling else problem, which is important enough to deserve a separate heading. See below.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"The_dangling_else_problem\"><\/span>The dangling else problem<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>This one brings us back to the nested if statements. Let&#8217;s take an example<\/p>\n<pre class=\"theme:github-copy toolbar:2 nums:false lang:c decode:true\">int age = 20;\r\nif (age &gt;= 18)\r\n    if (age &lt; 65)\r\n        printf(\"Price for full ticket: 10 euros\\n\");\r\nelse\r\n    printf(\"Minors are not allowed without a guardian\\n\");<\/pre>\n<p>Syntactically the following code is correct and compiles. It contains a nested if statement that will print out the full ticket price for everyone who is from 18 to 65. <strong>Functionally however it behaves incorrectly. This is what is known a dangling else issue.\u00a0<\/strong>In more layman&#8217;s terms &#8211; to which of the <span class=\"lang:c decode:true crayon-inline \">if<\/span>\u00a0 statements does the <span class=\"lang:c decode:true crayon-inline \">else<\/span> belong to? This is made worse by the deliberately indented code, that will make you think it&#8217;s correct. However just a reminder &#8211; C does not care about whitespace. It could all be written on the same line and it would still compile.<\/p>\n<p>If you have relevant warning messages enabled for the compiler, you will see a warning about this:<br \/>\n<span class=\"lang:c highlight:0 decode:true crayon-inline\">warning: suggest explicit braces to avoid ambiguous &#8216;else&#8217; [-Wdangling-else]|<\/span><\/p>\n<p><strong>Try the code out!<\/strong> Try it with the age set to 20. Then try with the age value of 80. As you can see, the else actually belongs to the last if statement, not the previous one as the indentation might suggest.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Refactoring_bad_nested_conditional_statements\"><\/span>Refactoring bad nested conditional statements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Even though it is possible to nest if-else statements together to form constructs where multiple conditions need to be true, in many cases you will end up with badly readable code. This code can become hard to trace, debug and may even have branches that will never or always be true.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    int age = 20;\r\n    if (age &gt;= 18)\r\n    {\r\n        if (age &lt; 65)\r\n        {\r\n            printf(\"Price for full ticket: 10 euros\\n\");\r\n        }\r\n        else\r\n        {\r\n            printf(\"Price for elderly: 4 euros\\n\");\r\n        }\r\n    }\r\n    else\r\n    {\r\n        printf(\"Minors are not allowed without a guardian\\n\");\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>This statement can be refactored in many ways. One way would be to reorder the conditions and introduce an <span class=\"lang:c decode:true crayon-inline \">else if<\/span>\u00a0 statement.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">if (age &lt; 18)\r\n    printf(\"Minors are not allowed without a guardian\\n\");\r\nelse if (age &gt;= 65)\r\n    printf(\"Price for elderly: 4 euros\\n\");\r\nelse\r\n    printf(\"Price for full ticket: 10 euros\\n\");\r\n<\/pre>\n<p>There are both benefits and disadvantages to this. The benefit would be, that it&#8217;s really easy to read. All specific cases are tested first and then what will be left is the generic case.<\/p>\n<p>The downside of this can be if the code needs to be well optimized for speed and the event is mainly for adults. This would make the code for the adult branch most likely to be executed, but all the other conditions will be tested first. This will cause the code to run slower. Refactoring often includes code execution speed or memory footprint optimizations, which tend to lie with theory of probability (according to the nature of our data, which condition is the most likely to occur).<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true \">if (age &gt;= 18 &amp;&amp; age &lt; 65)\r\n    printf(\"Price for full ticket: 10 euros\\n\");    \r\nelse if (age &gt;= 65)\r\n    printf(\"Price for elderly: 4 euros\\n\");\r\nelse\r\n    printf(\"Minors are not allowed without a guardian\\n\");\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Function_calls_in_conditionals\"><\/span>Function calls in conditionals<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To optimize code readability, you often will want to separate code into lots of small single-purpose functions. If these functions return a value, it can be checked within the if statement&#8217;s condition.<\/p>\n<p>We can use the comparison operators to compare the returned value from the function if it matches our criteria. Let&#8217;s take a function that will count the number of commas in a string.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">int NumOfCommas(char *str)\r\n{\r\n    int count = 0;\r\n    int i = 0;\r\n    while (str[i])\r\n    {\r\n        if (str[i] == ',')\r\n            count++;\r\n        i++;\r\n    }\r\n    return count;\r\n}<\/pre>\n<p>Now let&#8217;s call this function in an if statement.<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">char *t = \"Hello, world!\";\r\nif (NumOfCommas(t) &gt; 0)\r\n{\r\n    printf(\"The text has at least 1 comma!\\n\");\r\n}<\/pre>\n<p>You can of course use the previously introduced concepts and make it cleaner<\/p>\n<pre class=\"toolbar:2 nums:false lang:c decode:true\">char *t = \"Hello, world!\"; \r\nif (NumOfCommas(t))\r\n    printf(\"The text has at least 1 comma!\\n\");<\/pre>\n<p>You will see a lot of similarly written code in data validation, where you will have only true\/false return values for the functions.<\/p>\n<p>We could have functions such as <span class=\"lang:c decode:true crayon-inline \">SentenceBeginsWithChar()<\/span> , <span class=\"lang:c decode:true crayon-inline \">SentenceMoreThanOneWord()<\/span> , <span class=\"lang:c decode:true crayon-inline \">SentenceEndWithPunctuation()<\/span> , <span class=\"lang:c decode:true crayon-inline \">ContainsAVerb()<\/span>\u00a0 etc &#8211; if all are considered true, then it will count it as a possibly a correctly formed sentence.<\/p>\n<p>As usual, this can be combined with other conditional statements. You can also have multiple calls to functions in the conditional statement, as long as you formulate it according to the rules of the language.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Assignments_in_conditionals\"><\/span>Assignments in conditionals<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This concept is more used within loops, but also appears in tasks such as data validation. It can be considered a 2-in-1 operation, so instead of doing an assignment before and checking the value later, you combine the two.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Ternary_operator\"><\/span>Ternary operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>TODO<\/p>\n<p>Ternary operator is essentially a short way of writing an if-else statement. It has the benefit that it can be embedded into code where an if-else statement cannot, hence it&#8217;s also sometimes called an inline if. By language syntax, the following is the form for the statement, with symbols such as ? and : separating expressions.<\/p>\n<p data-select-like-a-boss=\"1\"><span class=\"lang:c decode:true crayon-inline\">expression ? expression : expression;<\/span><\/p>\n<p>However this is not readable unless you know what you are looking at. Let&#8217;s write it out a bit more clearly<\/p>\n<p><span class=\"lang:c decode:true crayon-inline \">(condition_to_check) ? expression(s)_if_true : expression(s)_if_false;<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"De_Morgans_law\"><\/span>De Morgan&#8217;s law<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>TODO<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the comprehensive and overly long page about writing a basic conditional statement. Introduction Conditional statements are the most basic way of managing program&#8217;s control flow. In a more literal sense, it enables altering which actions the program takes, depending on the conditions that we check against. For example consider an event ticketing system, &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/conditional-statements\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Conditional statements<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4802,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-4805","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=4805"}],"version-history":[{"count":13,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4805\/revisions"}],"predecessor-version":[{"id":8814,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4805\/revisions\/8814"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4802"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=4805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}