{"id":2265,"date":"2024-05-10T07:17:18","date_gmt":"2024-05-10T07:17:18","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2265"},"modified":"2024-05-10T07:17:18","modified_gmt":"2024-05-10T07:17:18","slug":"c-if-statement","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-if-statement\/","title":{"rendered":"C \u2013 If 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-statement\">C \u2013 If Statement<\/a><\/li><li ><a href=\"#what-is-if-in-c\">What is if in C?<\/a><\/li><li ><a href=\"#syntax-of-if-statement-in-c\">Syntax of if Statement in C<\/a><\/li><li ><a href=\"#how-to-use-if-statement-in-c\">How to use if statement in C?<\/a><\/li><li ><a href=\"#how-if-in-c-work\">How if in C work?<\/a><\/li><li ><a href=\"#examples-of-if-statements-in-c\">Examples of if Statements in C<\/a><\/li><li ><a href=\"#advantages-of-if-statement\">Advantages of if statement <\/a><\/li><li ><a href=\"#disadvantages-of-if-statement\">Disadvantages of if Statement<\/a><\/li><li ><a href=\"#faq-c-if-statement\">FAQ-  C \u2013 If Statement<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-if-statement\">C \u2013 If Statement<\/h2>\n\n\n\n<p>The &#8220;if&#8221; statement is a fundamental control structure in the C programming language that enables the execution of specific code blocks based on a conditional expression. It plays a pivotal role in decision-making within C programs, allowing developers to create dynamic and responsive applications. With the &#8220;if&#8221; statement, programmers can instruct the program to execute certain instructions when a given condition is true while providing an alternative set of instructions when the condition is false. In this article, we will delve into the syntax, usage, and versatility of the &#8220;if&#8221; statement, exploring how it empowers C programmers to build logic-driven and efficient software solutions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-if-in-c\">What is if in C?<\/h2>\n\n\n\n<p>In C programming, the &#8220;if&#8221; statement is like a traffic signal. It helps your program make decisions by running different code based on a condition. It&#8217;s a fundamental tool for adding choices to your program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-if-statement-in-c\">Syntax of if Statement in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if(condition) \n{\n    \/\/ if body\n    \/\/ Statements to execute if condition is true\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-if-statement-in-c\">How to use if statement in C?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the syntax of if statement\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    int gfg = 9;\n    \/\/ if statement with true condition\n    if (gfg &lt; 10) {\n        printf(\"%d is less than 10\", gfg);\n    }\n \n    \/\/ if statement with false condition\n    if (gfg &gt; 20) {\n        printf(\"%d is greater than 20\", gfg);\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>9 is less than 10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-if-in-c-work\">How if in C work?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: The program checks a condition in the &#8220;if&#8221; statement.<\/li>\n\n\n\n<li><strong>Step 2A<\/strong>: If the condition is true, it runs the code inside the &#8220;if&#8221; block.<\/li>\n\n\n\n<li><strong>Step 2B<\/strong>: If the condition is false, it skips the code inside the &#8220;if&#8221; block.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: After the &#8220;if&#8221; block, the program continues with the code outside of it.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-if-statements-in-c\">Examples of if Statements in C<\/h2>\n\n\n\n<p><strong>Example 1: C Program to check whether the number is even or odd.<\/strong><\/p>\n\n\n\n<p>In this program, we&#8217;re checking whether a number is even (divisible by 2) or odd (not divisible by 2), except for one specific case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to check if the number is even or odd\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    int n = 4956;\n \n    \/\/ condition to check for even number\n    if (n % 2 == 0) {\n        printf(\"%d is Even\", n);\n         \n    }\n \n    \/\/ condition to check for odd number\n    else {\n        printf(\"%d is Odd\", n);\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>4956 is Even\n<\/code><\/pre>\n\n\n\n<p><strong>Example 2: C Program to check whether a number is prime or not.<\/strong><\/p>\n\n\n\n<p>This program checks the smallest factor of the given number from 2 to sqrt N using a loop. Whenever there is a factor, ultimately we&#8217;ll set the flag and exit the loop.<\/p>\n\n\n\n<p>The code to be executed will be put inside the if statement<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to check whether a number is prime or not\n#include &lt;math.h&gt;\n \nint main()\n{\n    int n = 19;\n    int flag = 0;\n \n    for (int i = 2; i * i &lt;= n; i++) {\n \n        \/\/ If n is divisible by any number between\n        \/\/ 2 and n\/2, it is not prime\n        if (n % i == 0) {\n            flag = 1;\n            break;\n        }\n    }\n \n    printf(\"%d is \", n);\n    if (flag == 1) {\n        \/\/ it is only printed if the number is not prime\n        printf(\"not \");\n    }\n    printf(\"a prime number.\\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>19 is a prime number.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-if-statement\">Advantages of if statement <\/h2>\n\n\n\n<p>The following are the advantages of if statement in C<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Simple Decision-Making Statement:<\/strong> The <code>if<\/code> statement provides a straightforward way to make decisions in your code. It allows you to specify a condition, and if that condition is true, the associated block of code is executed.<\/li>\n\n\n\n<li><strong>Ease of Use and Understanding:<\/strong> <code>if<\/code> statements are easy to read and understand, making your code more human-readable. They are a fundamental concept in programming, and most developers are familiar with their usage.<\/li>\n\n\n\n<li><strong>Evaluate Expressions of All Types:<\/strong> <code>if<\/code> statements are versatile and can evaluate expressions of various data types, including integers, characters, booleans, and more. As long as the condition provided evaluates to either <code>True<\/code> or <code>False<\/code>, it can be used in an <code>if<\/code> statement.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-if-statement\">Disadvantages of if Statement<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Single Block:<\/strong> In a typical <code>if-elif-else<\/code> structure, each condition is checked one by one until a true condition is found, and then the associated block is executed. Once a matching <code>if<\/code> or <code>elif<\/code> block is found, subsequent conditions are not tested. So, it does not necessarily test all blocks when a match is found at the start.<\/li>\n\n\n\n<li><strong>Complexity and Readability:<\/strong> When you have a large number of conditions and associated code blocks, the code can become complex and less readable. This is a valid concern, and it&#8217;s important to structure your code effectively to maintain readability. In such cases, you might consider using other constructs like dictionaries or switch-case statements if they are available in your programming language to improve code organization.<\/li>\n\n\n\n<li><strong>Performance for Many Conditions:<\/strong> In some situations, especially when dealing with a large number of conditions, using multiple <code>if<\/code> statements can lead to slower performance compared to other data structures optimized for this purpose, like hash tables or decision trees. However, the impact on performance largely depends on the programming language and the specific use case. In practice, for most everyday programming tasks, the performance difference is negligible.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-if-statement\">FAQ-  C \u2013 If 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-1696328830792\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to use if function in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>STEP 1:<\/strong> Evaluate the <code>if<\/code> condition.<br \/><strong>STEP 2A:<\/strong> If the condition is true, execute the code inside the <code>if<\/code> block.<br \/><strong>STEP 2B:<\/strong> If the condition is false, skip the <code>if<\/code> block and continue with the next code.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696328842641\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is a compound if statement in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To execute multiple statements when an <code>if<\/code> condition is true, enclose them within braces <code>{}<\/code> to create a block.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696328851834\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are the selection statements in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>Selection Statements:<\/strong><br \/><code>if<\/code> and <code>switch<\/code>: Used for conditional branching based on expressions.<br \/><strong>Iteration Statements:<\/strong><br \/><code>while<\/code>, <code>do<\/code>, and <code>for<\/code>: Create loops to execute code based on conditions.<br \/><strong>Jump Statements:<\/strong><br \/><code>break<\/code>, <code>continue<\/code>, and <code>goto<\/code>: Control program flow within loops and conditions.<br \/><code>return<\/code>: Exit functions and optionally return values.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C \u2013 If Statement The &#8220;if&#8221; statement is a fundamental control structure in the C programming language that enables the execution of specific code blocks based on a conditional expression. It plays a pivotal role in decision-making within C programs, allowing developers to create dynamic and responsive applications. With the &#8220;if&#8221; statement, programmers can instruct &#8230; <a title=\"C \u2013 If Statement\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-if-statement\/\" aria-label=\"More on C \u2013 If Statement\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5337,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[404],"class_list":["post-2265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-if-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\/2265","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=2265"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2265\/revisions"}],"predecessor-version":[{"id":10644,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2265\/revisions\/10644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5337"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}