If Statement in C- Programming
If Statement in C- Programming
The if in C is the most simple decision-making statement. It consists of the test condition and if
block or body. If the given condition is true only then the if block will be executed.
What is if in C?
The if in C is a decision-making statement that is used to execute a block of code based on the
value of the given expression. It is one of the core concepts of C programming and is used to include
conditional code in our program.
Syntax of if Statement in C
if( condi tion)
// if body
// Statementsto execute if condition is true
int main ()
int g fg
if statementwith true condition
(gfg < 10)
printf( is less than 10', ZfZ)•
return O ;
Output
9 is less than 10
How if in C works?
//Statements; //Statements;
False
Condition
True
if Condition
username code
Examples of if Statements in C
In this program, we will make use of the logic that if the number is divisible by 2, then it is even else
odd except one.
c
Program to check if the number is even or odd
#include < stdio.h>
int main ( )
int n 4956;
return O;
Output
4956 is Even
Example 2: C Program to check whether a number is prime or not.
In this program, we will check for the smallest factor of the given number N starting from 2 to sqrt
(N) using a loop. Whenever we find the factor, we will set the flag and exit the loop. The code to be
executed will be contained inside the if statement.
int main ()
int n
int flag o;
for (int
printf( • is "
if (flag
// it is only printed if the number is not prime
printf( "not
prime number.\n')•
return O ;
Output
19 is a prime number.
Advantages of if Statement
Following are the main advantages of the if statement in C:
• It is the simplest decision-making statement.
• It is easy to use and understand.
• It can evaluate expressions of all types such as int, char, bool, etc.
Disadvantages of if Statement
The main limitations of if block is listed below:
• It contains only a single block. In case when there are multiply related if blocks, all the blocks
will be tested even when the matching if block is found at the start
• When there are a large number of expressions, the code of the if block gets complex and
unreadable.
• It is slower for a large number of conditions.
Conclusion
The if statement is the simplest decision-making statement due to which it is easy to use and
understand. But being simple, it also has many limitations. We can use if-else, if-else-if ladder, or
switch statements to overcome these limitations. Still, the if statement is widely used in C
programming to add some conditional code to the program.
Advantages of if Statement
Following are the main advantages of the if statement in C:
Disadvantages of if Statement
The main limitations of if block is listed below:
• It contains only a single block. In case when there are multiply related if blocks, all the blocks
will be tested even when the matching if block is found at the start
• When there are a large number of expressions, the code of the if block gets complex and
unreadable.
• It is slower for a large number of conditions.
Conclusion
The if statement is the simplest decision-making statement due to which it is easy to use and
understand. But being simple, it also has many limitations.We can use if-else, if-else-if ladder, or
switch statements to overcome these limitations. Still, the if statement is widely used in C
programmingto add some conditional code to the program.
FAQs on if in C
1. Define C if staement.
The if statement is a program control statement in C language that is used to execute a part of code
based on some condition.
We can specify multiple conditions in the if statement but not separately. We have to join these
multiple conditions using logical operators making them into a single expression. We can then use
this expression in the if statement.
Valid Expressions
Invalid Expressions
In the above expression, the rightmost expression in the parenthesis will be considered.