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<iostream.h>
#include<conio.h>
void main()
{
 int num;
cout<<"Enter a number less than 10: ";
cin>>num;
if(num <= 10) //if 'num' value Less then or Equal to 10 then
{
cout<<"What an obedient servant you are!"; //print this.
}
getch();
}
Output
Enter a number less than 10: 7
What an obedient servant you are!

Using If Statement Checking user Enter Value
Program

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter a number less than 10: ";
cin>>num; //user enter value storing in 'num'.
if(num < 10) //if 'num' value is less then 10 then
{
cout<<"Entered value is Less then 10!"; //print this.
}
if(num == 10) //if 'num' value is equal to 10 then
{
cout<<"Entered value is Equal to 10!"; //print this.
}
if(num > 10) //if 'num' value greater then 10 then
{
cout<<"Entered value is greater then 10"; //print this.
}
getch();
}
Output
Enter a number less than 10: 11
Entered value is greater then 10