The syntax of the if
statement in C programming is:
if (test expression)
{
// code
}
How if statement works?
The if
statement evaluates the test expression inside the parenthesis ()
.
- If the test expression is evaluated to true, statements inside the body of
if
are executed. - If the test expression is evaluated to false, statements inside the body of
if
are not executed.

Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2 You entered -2. The if statement is easy.
When the user enters -2, the test expression number<0
is evaluated to true. Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5 The if statement is easy.
When the user enters 5, the test expression number<0
is evaluated to false and the statement inside the body of if
is not executed.