C - if Statement
Last Updated : 28 Sep, 2023
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
How to use if statement in C?
The following examples demonstrate how to use the if statement in C:
// C Program to demonstrate the syntax of if statement
kinclude < stdio.h>
int main ()
int g fg
if statementwith true condition
(gfg < 10)
printf( is less than 10', ZfZ)•
if statement with false condition
(gfg > 20)
printf( is greater than 20' , ZfZ);
return O ;
Output
9 is less than 10
How if in C works?
Expression is True. Expression is False.
Int GFG = 9; Int GFG = 9;
if (GFG < 10) if (GFG > 10)
//Statements; //Statements;
// code after if // code after if
Working Ofif Statement in C
The working of the if statement in C is as follows:
1. STEP 1: When the program control comes to the if statement, the test expression is evaluated.
2. STEP 2A: If the condition is true, the statements inside the if block are executed.
3. STEP 2B: If the expression is false, the statements inside the if body are not executed.
4. STEP 3: Program control moves out of the if block and the code after the if block is executed.
Flowchart of if in C
False
Condition
True
if Condition
username code
Flow Diagram Ofif Statement in C
Examples of if Statements in C
Example 1: C Program to check whether the number is even or odd.
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;
// condition to check for even number
if (n 2
printf( is Even" , n);
// condition to check for odd number
else {
printf( "Ed is Odd", n);
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.
// C program to check whether a number is prime or not
kinclude<math.h>
int main ()
int n
int flag o;
for (int
If n is divisible any number between
2 and n/ 2, it is nat prime
(n 70 i
flag 1;
break;
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:
• 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
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.
2. How many types of decision-making statements are there in the C language?
There are 5 types of conditional statements or decision-making statements in C language:
1. if Statement
2. if-else Statement
3. if-else-if Ladder
4. switch Statement
5. Conditional OQ.e.@.tQL
3. Can we specify multiple conditions in if statement?
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
if (a < b a < c);
if (a 25 Il a < 25);
Invalid Expressions
if (a < b, a < c);
In the above expression, the rightmost expression in the parenthesis will be considered.