{"id":70,"date":"2023-09-11T06:40:08","date_gmt":"2023-09-11T05:40:08","guid":{"rendered":"https:\/\/onlinecodinghelp.com\/?p=70"},"modified":"2023-09-12T08:33:50","modified_gmt":"2023-09-12T07:33:50","slug":"conditional-statement","status":"publish","type":"post","link":"https:\/\/onlinecodinghelp.com\/index.php\/2023\/09\/11\/conditional-statement\/","title":{"rendered":"1. There are many Conditional Statement Blogs but Most exciting one is here"},"content":{"rendered":"\n<p>A &#8220;Conditional Statement&#8221; in programming is a fundamental construct that allows a program to make decisions based on certain conditions or criteria. These statements enable a program to execute different sets of instructions depending on whether a specified condition is true or false. What is if in simple word is <a href=\"https:\/\/www.englishgrammar.org\/sentences\/\" data-type=\"link\" data-id=\"https:\/\/www.englishgrammar.org\/sentences\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"500\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Art-Day.png\" alt=\"Conditional Statement\" class=\"wp-image-93\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Art-Day.png 1024w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Art-Day-300x146.png 300w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/Art-Day-768x375.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-if-conditional-statement\">What is <em>if<\/em> conditional statement?<\/h2>\n\n\n\n<p style=\"font-size:15px\">If statements are generally used as decision making statements. Which is used to execute a block based on the value of the given condition. it is core concept of the any programming language. <\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Different conditional statements are as below<\/h2><nav><ul><li class=\"\"><a href=\"#what-is-if-conditional-statement\">What is if conditional statement?<\/a><\/li><li class=\"\"><a href=\"#1-simple-if-conditional-statement\">1. Simple if  conditional statement<\/a><ul><li class=\"\"><a href=\"#syntax-of-if-conditional-statement\">Syntax of if conditional statement<\/a><\/li><li class=\"\"><a href=\"#flow-chart-of-if-statement\">Flow Chart of If statement :<\/a><\/li><li class=\"\"><a href=\"#how-it-works\">How it works?<\/a><\/li><li class=\"\"><a href=\"#how-to-use-if-statement-in-java-c-c\">How to use if statement in Java\/C\/C++ ?<\/a><ul><li class=\"\"><a href=\"#output\">Output :<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#when-to-use\">When to use?<\/a><\/li><li class=\"\"><a href=\"#advantages-of-if-statements\">Advantages of if statements :<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#2-if-else-statement\">2. If-Else Statement<\/a><ul><li class=\"\"><a href=\"#syntax-of-if-else-conditional-statement\">Syntax of if-else conditional statement<\/a><\/li><li class=\"\"><a href=\"#flow-chart-of-if-else-statement\">Flow chart of If-Else statement<\/a><\/li><li class=\"\"><a href=\"#how-it-works-1\">How it works?<\/a><\/li><li class=\"\"><a href=\"#how-to-use-if-statement-in-java-c-c-2\">How to use if statement in Java\/C\/C++ ?<\/a><ul><li class=\"\"><a href=\"#output-3\">Output :<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#when-to-use-4\">When to use?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-simple-if-conditional-statement\">1. Simple if  conditional statement<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax-of-if-conditional-statement\">Syntax of if conditional statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>if(condition\/expression){\n     \/\/ Body - Statements inside the block will execute if the given condition is true\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"flow-chart-of-if-statement\">Flow Chart of If statement :<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"272\" height=\"492\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/if-3.png\" alt=\"\" class=\"wp-image-86\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/if-3.png 272w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/if-3-166x300.png 166w\" sizes=\"(max-width: 272px) 100vw, 272px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-it-works\">How it works?<\/h3>\n\n\n\n<p>Step-1 : Start of the program.<br>Step-2 : Check the given expression\/condition whether it is satisfied or not.<br>Step-3 : Optional &#8211; if the given condition is satisfied then execute block of statement<br>Step-4 : Continue processing other statements<br>Step-5 : End of Program<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-use-if-statement-in-java-c-c\">How to use if statement in Java\/C\/C++ ?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ - Java Programming Example \npublic class Main {\n    public static void main(String&#91;] args) {\n        \/\/ TODO: Example of If statement\n        System.out.println(\"First Example\");\n        if(5 &gt; 3){ \/\/ is 5 more than 3 -- Yes - returns true\n            System.out.println(\"5 is bigger than 3\");\n        }\n        System.out.println(\"End of first example\");\n        \n    }\n}\n\n\n\/\/ - C\/C++  programming Example \n\nint main()\n{\n        \/\/ TODO: Example of If statement\n        printf(\"First Example\\n\");\n        if(5 &gt; 3){ \/\/ is 5 more than 3 -- Yes - returns true\n            printf(\"5 is bigger than 3\\n\");\n        }\n        printf(\"End of first example\");\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output\">Output :<\/h4>\n\n\n\n<p>First Example<br>5 is bigger than 3<br>End of First example<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"when-to-use\">When to use?<\/h3>\n\n\n\n<p>When a programmer want to execute any block of code based on a condition or conditions then they can use if statement as decision making statement. if the given condition will satisfied then given block of code will be executed and populate result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"advantages-of-if-statements\">Advantages of if statements :<\/h3>\n\n\n\n<ul>\n<li>It&#8217;s very easy to use and understand<\/li>\n\n\n\n<li>It&#8217;s simple decision making statement.<\/li>\n\n\n\n<li>it can evaluate all type of expressions.<\/li>\n<\/ul>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-if-else-statement\">2. If-Else Statement<\/h2>\n\n\n\n<p>if-else statement is worked as <strong><em>either-or<\/em><\/strong> condition. if the given condition is <em>true<\/em> then it will execute block of code else it will execute else block.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax-of-if-else-conditional-statement\">Syntax of if-else conditional statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>if(condition\/expression){\n     \/\/ Body - Statements inside the block will execute if the given condition is true\/correct\n}\nelse{\n    \/\/ Body - Statements inside the block will execute if the given condition is false\/wrong\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"flow-chart-of-if-else-statement\">Flow chart of If-Else statement<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"396\" height=\"492\" src=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/IF_ELSE-2.png\" alt=\"\" class=\"wp-image-111\" srcset=\"https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/IF_ELSE-2.png 396w, https:\/\/onlinecodinghelp.com\/wp-content\/uploads\/2023\/09\/IF_ELSE-2-241x300.png 241w\" sizes=\"(max-width: 396px) 100vw, 396px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-it-works-1\">How it works?<\/h3>\n\n\n\n<p>Step-1 : Start of the program.<br>Step-2 : Check the given expression\/condition whether it is satisfied or not.<br>Step-3 : Conditional &#8211; if the given condition is satisfied and it return <strong><em>true<\/em><\/strong> then execute block A<br>Step-4 : Conditional &#8211; if the given condition is not satisfied and it return <strong><em>false<\/em><\/strong> then execute block B<br>Step-5 : End of Program<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-use-if-statement-in-java-c-c-2\">How to use if statement in Java\/C\/C++ ?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ - Java Programming Example \npublic class Main {\n    public static void main(String&#91;] args) {\n        \/\/ TODO: Example of If statement\n        System.out.println(\"First Example\");\n        if(5 &gt; 3){ \/\/ is 5 more than 3 -- Yes - returns true\n            System.out.println(\"5 is bigger than 3\");\n        }else{\n            System.out.println(\"3 is smaller than 5\");\n        }\n        System.out.println(\"End of first example\");\n\n        System.out.println(\"---------------------------------\");\n        System.out.println(\"Second Example\");\n        if(5 &lt; 3){ \/\/ is 3 more than 5 -- No - returns False\n            System.out.println(\"5 is bigger than 3\");\n        }else{\n            System.out.println(\"3 is smaller than 5\");\n        }\n        System.out.println(\"End of first example\");\n    }\n}\n\n\n\/\/ - C\/C++  programming Example \n\nint main()\n{\n        \/\/ TODO: Example of If statement\n        printf(\"First Example\\n\");\n        if(5 &gt; 3){ \/\/ is 5 more than 3 -- Yes - returns true\n            printf(\"5 is bigger than 3\\n\");\n        }else {\n            printf(\"3 is smaller than 5\\n\");\n        }\n\n        printf(\"---------------------------------\\n\");\n        printf(\"First Example\\n\");\n        if(5 &gt; 3){ \/\/ is 5 more than 3 -- Yes - returns true\n            printf(\"5 is bigger than 3\\n\");\n        }else {\n            printf(\"3 is smaller than 5\\n\");\n        }\n        printf(\"End of first example\");\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output-3\">Output :<\/h4>\n\n\n\n<p>First Example<br>5 is bigger than 3<br>End of first example<br>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br>Second Example<br>3 is smaller than 5<br>End of first example<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"when-to-use-4\">When to use?<\/h3>\n\n\n\n<p>When there is a scenario where programmer want to execute block of code based on either-or codition then they can use if-else statement. if the given condition is true then block A will be executed or block B will be executed.<\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-3 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A &#8220;Conditional Statement&#8221; in programming is a fundamental construct that allows a program to make&#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,14,3],"tags":[20,13,11,12,21],"_links":{"self":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":20,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onlinecodinghelp.com\/index.php\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}