If Statement in C

If statement in C programming allows you to control and execute a specific block of code if the condition is true or false based on some specific condition.

General form of If Statement


if(this condition is true)
execute this statement;

If Statement Expression

this expression is true if
x == y x is equal to y
x != y x is not equal to y
x < y x is less than y
x > y x is greater than y
x <= y x is less than or equal to y
x >= y x is greater than or equal to y

Using If Statement

Program

#include<stdio.h>
#include<conio.h>
void main()
{
 int num;
printf("Enter a number less than or equal to 10: ");
scanf("%d", &num);
if(num <= 10) //if 'num' value Less then or Equal to 10 then
{
printf("What an obedient servant you are!"); //print this.
}
getch();
}
Output
Enter a number less than or equal to 10: 8
What an obedient servant you are!

Using If Statement Checking user Enter Value
Program

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter a number less than 10: ");
scanf("%d", &num); //user enter value storing in 'num'.
if(num < 10) //if 'num' value is less then 10 then
{
printf("Entered value is Less then 10!"); //print this.
}
if(num == 10) //if 'num' value is equal to 10 then
{
printf("Entered value is Equal to 10!"); //print this.
}
if(num > 10) //if 'num' value greater then 10 then
{
printf("Entered value is Greater then 10"); //print this.
}
getch();
}
Output
Enter a number less than 10: 22
Entered value is Greater then 10