{"id":2270,"date":"2024-05-10T07:17:27","date_gmt":"2024-05-10T07:17:27","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2270"},"modified":"2024-05-10T07:17:27","modified_gmt":"2024-05-10T07:17:27","slug":"c-ifelse-statement","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-ifelse-statement\/","title":{"rendered":"C If\u2026Else Statement"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#c-if-else-statement\">C If\u2026Else Statement<\/a><\/li><li ><a href=\"#c-if-statement\">C if Statement<\/a><\/li><li ><a href=\"#c-if-else-statement-1\">C if-else Statement<\/a><\/li><li ><a href=\"#syntax-of-if-else\">Syntax of if-else<\/a><\/li><li ><a href=\"#how-to-use-if-else-in-c\">How to use if-else in C?<\/a><\/li><li ><a href=\"#how-if-else-statement-work\">How if-else Statement work?<\/a><\/li><li ><a href=\"#examples-of-if-else-statement-in-c\">Examples of if-else Statement in C<\/a><\/li><li ><a href=\"#advantages-of-if-else-statement\">Advantages of if-else Statement<\/a><\/li><li ><a href=\"#disadvantages-of-if-else-statement\">Disadvantages of if-else Statement<\/a><\/li><li ><a href=\"#faq-c-if-else-statement\">FAQ- C If\u2026Else Statement<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-if-else-statement\">C If\u2026Else Statement<\/h2>\n\n\n\n<p>The &#8220;if\u2026else&#8221; statement in the C programming language is a fundamental control structure that allows you to make decisions and control the flow of your program based on certain conditions. With &#8220;if\u2026else,&#8221; you can specify two different blocks of code to be executed, one when a given condition is true, and another when the condition is false. This conditional statement is pivotal in enabling your program to make intelligent choices and respond differently to various situations, enhancing its versatility and functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-if-statement\">C if Statement<\/h2>\n\n\n\n<p>The&nbsp;if statement&nbsp;in C functions to execute a block of code based on a specified condition.<\/p>\n\n\n\n<p>Syntax of C if statement in C<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\n   \/\/ code to be executed if the condition is true\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-if-else-statement-1\">C if-else Statement<\/h2>\n\n\n\n<p>The if-else statement is a way to make decisions in programming. It checks a condition, like a true or false question. If the condition is true, it does one thing (inside the &#8220;if&#8221; part). If the condition is false, it does another thing (inside the &#8220;else&#8221; part). This helps programs react differently to different situations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-if-else\">Syntax of if-else<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\n    \/\/ code executed when the condition is true\n}\nelse {\n    \/\/ code executed when the condition is false\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-if-else-in-c\">How to use if-else in C?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the use of if-else statement\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ if block with condition at the start\n    if (5 &lt; 10) {\n \n        \/\/ will be executed if the condition is true\n        printf(\"5 is less than 10.\");\n    }\n \n    \/\/ else block after the if block\n    else {\n \n        \/\/ will be executed if the condition is false\n        printf(\"5 is greater that 10.\");\n    }\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5 is less than 10.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-if-else-statement-work\">How if-else Statement work?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It checks the test condition.<\/li>\n\n\n\n<li>If the condition is true, it executes the code inside the if block.<\/li>\n\n\n\n<li>If the condition is false, it executes the code inside the else block (if there is one).<\/li>\n\n\n\n<li>Afterward, the program continues to execute statements below the if-else statement, regardless of which block was executed.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-if-else-statement-in-c\">Examples of if-else Statement in C<\/h2>\n\n\n\n<p><strong>Example 1: C Program to check whether a given number is even or odd<\/strong><\/p>\n\n\n\n<p>A Number should be divided by 2, in order to become even. Hence, we will use the if-else statement to check the condition and execute different statements for when it is true and when it is false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to Demonstrate the working of if-else statement\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ Some random number\n    int num = 9911234;\n \n    \/\/ checking the condition at the start of if block\n    if (num % 2 == 0) {\n        \/\/ executed when the number is even\n        printf(\"Number is even\");\n    }\n    \/\/ else block\n    else {\n        \/\/ executed when the number is odd\n        printf(\"Number is Odd\");\n    }\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number is even\n<\/code><\/pre>\n\n\n\n<p><strong>Example 2. C Program to check whether a person is eligible to vote or not.<\/strong><\/p>\n\n\n\n<p>A  person is eligible to vote after he\/she is at least 18 years old. Therefore, we use this condition in the if-else statement to check the eligibility of the person.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to check whether the person is eligible to vote\n\/\/ or not\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ declaring age of two person\n    int p1_age = 15;\n    int p2_age = 25;\n \n    \/\/ checking eligibility of person 1\n    if (p1_age &lt; 18)\n        printf(\"Person 1 is not eligible to vote.\\n\");\n    else\n        printf(\"Person 1 is eligible to vote.\\n\");\n \n    \/\/ checking eligiblity of person 2\n    if (p2_age &lt; 18)\n        printf(\"Person 2 is not eligible to vote.\\n\");\n    else\n        printf(\"Person 2 is eligible to vote.\");\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Person 1 is not eligible to vote.\nPerson 2 is eligible to vote.<\/code><\/pre>\n\n\n\n<p>In the second example, we can notice that they didn&#8217;t put braces for the if and else statements. Even though, the code is running without error. This occurs due to the special property of the C language, that is, it allows the skipping of the braces around the body of the if-else statement when there is only one statement in the body.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-if-else-statement\">Advantages of if-else Statement<\/h2>\n\n\n\n<p>The if-else statement allows users to execute various statements based on different conditions. It can evaluate test expressions of various types, including int, char, and boolean. This statement is valuable for altering the program&#8217;s flow, making it more versatile. It&#8217;s simple, efficient, and easier to read when dealing with a smaller number of conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-if-else-statement\">Disadvantages of if-else Statement<\/h2>\n\n\n\n<p>When there are numerous if statements in the code, it can become challenging to read and maintain, leading to complexity and reduced readability. Additionally, performance may be impacted, and the code can become slower when compared to using a switch statement, especially if there are many conditions to evaluate.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-if-else-statement\">FAQ- C If\u2026Else Statement<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696335219907\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are the 4 types of if statements in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. There are four types of if Statement in c:\u00a0simple if, if-else, nested if-else,<strong> <\/strong>and else-if ladder. In C, if statement supports two-way branching statement and multi-way branching statement.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696335225353\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is if statement syntax?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The syntax of an &#8216;if&#8217; statement in C programming language is \u2212\u00a0<strong>if(boolean_expression) { \/* statement(s) will execute if the boolean expression is true *\/ }<\/strong>\u00a0If the Boolean expression evaluates to true, then the block of code inside the &#8216;if&#8217; statement will be executed.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696335229202\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Is an if-else a loop?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In programming, <code>for<\/code> loops are used for repetitive tasks, executing code a set number of times. <code>if\/else<\/code> statements, on the other hand, are conditional, making decisions based on true or false conditions. They serve different purposes and aren&#8217;t in a precedence order; their use depends on the specific needs of your code.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><br><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>C If\u2026Else Statement The &#8220;if\u2026else&#8221; statement in the C programming language is a fundamental control structure that allows you to make decisions and control the flow of your program based on certain conditions. With &#8220;if\u2026else,&#8221; you can specify two different blocks of code to be executed, one when a given condition is true, and another &#8230; <a title=\"C If\u2026Else Statement\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-ifelse-statement\/\" aria-label=\"More on C If\u2026Else Statement\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5339,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[405],"class_list":["post-2270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-ifelse-statement","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2270"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2270\/revisions"}],"predecessor-version":[{"id":10645,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2270\/revisions\/10645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5339"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}