The if statement is the simplest decision-making statement in C. It executes a block of code only when the specified condition evaluates to true.
- If the condition is true, the statements inside the if block are executed.
- If the condition is false, the if block is skipped and execution continues with the next statement.
#include <stdio.h>
int main() {
int n = 9;
if (n < 10) {
printf("%d < 10", n);
}
if (n > 20) {
printf("%d > 20", n);
}
return 0;
}
Output
9 < 10
Explanation
In the above example, each if statement evaluates its condition independently.
- The first if condition (n < 10) is true, so its block is executed.
- The second if condition (n > 20) is false, so its block is skipped.
Syntax
if (condition) {
// if body
// Statements to execute if condition is true
}
Working of if Statement

The working of the if statement in C is as follows:
- STEP 1: When the program control comes to the if statement, the test expression is evaluated.
- STEP 2A: If the condition is true, the statements inside the if block are executed.
- STEP 2B: If the expression is false, the statements inside the if body are not executed.
- STEP 3: Program control moves out of the if block and the code after the if block is executed.
Flowchart of if in C

Examples of if Statements
The below examples demonstrate the use of if statement to execute conditional code in a C program
Check for Negative Number
We can check whether the number is negative by comparing it with 0 and checking if it is less than zero, it is negative.
#include <stdio.h>
int main() {
int n = -4;
// Condition to check for negative number
if (n < 0) {
printf("%d is Negative", n);
}
return 0;
}
Output
-4 is Negative
Check if an Integer Lies in the Range
To check whether the number is in the given range, we will need to combine two conditions. One to check for upper limit and one to check for lower limit.
#include <stdio.h>
int main() {
int n = 11;
// Check whether n is the range [10, 20]
if (n <= 20 && n >= 10)
printf("%d is in the range [10, 20]", n);
return 0;
}
Output
11 is in the range [10, 20]
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.
#include <stdio.h>
int main() {
int n = 4;
// Condition to check for even number
if (n % 2 == 0) {
printf("%d is Even", n);
}
// Condition to check for odd number
if (n % 2 != 0) {
printf("%d is Odd", n);
}
return 0;
}
Output
4 is Even