If-else statement in C++ programming allows you to control and execute the specific block of code if the condition is true, otherwise, execute the else block.
#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 or Equal to 10 then
{
cout<<"What an obedient servant you are!"; //print this.
}
else //if not mean 'num' value greater then 10 then
{
cout<<"Invalid Entry U Enter Greater then 10"; //print this.
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string> //including Library for Working with String.
void main()
{
string str, str2; //'str' and 'str2' used for String value.
str2 = "Khan"; //assigning value to 'str2'.
cout<<"Please Enter Ur Name: ";
cin>>str; //user Enter value storing in 'str'.
if(str == str2) //if 'str' value Equal to 'str2' then
{
cout<<"Welcome!"; //print this.
}
else //if not then
{
cout<<"I Dont Know U"; //print this.
}
getch();
}
#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 greater then 10 then
{
cout<<"Entered value is greater then 10!"; //print this.
}
else if(num < 10) //if 'num' value is Less then 10 then
{
cout<<"Entered value is Less then 10!"; //print this.
}
else //if not in above mean equal to 10 then
{
cout<<"Entered value is Equal to 10"; //print this.
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter value from 1 to 4: ";
cin>>num; //user enter value storing in 'num'.
if(num == 1) //if 'num' value equal to 1 then
{
cout<<"Entered value is 1!"; //print this.
}
else if(num == 2) //if 'num' value equal to 2 then
{
cout<<"Entered value is 2!"; //print this.
}
else if(num == 3) //if 'num' value equal to 3 then
{
cout<<"Entered value is 3!"; //print this.
}
else if(num == 4) //if 'num' value equal to 4 then
{
cout<<"Entered value is 4!"; //print this.
}
else //if not in above then
{
cout<<"Invalid Entry!"; //print this.
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter value from 1 to 3: ";
cin>>num; //user enter value saving in 'num'.
if(num <= 3) //if 'num' value Less then or equal to 3 then
{
if(num == 1) //if 'num' value equal to 1 then
{
cout<<"Entered value is 1"; //print this.
}
else if(num == 2) //if 'num' value equal to 2 then
{
cout<<"Entered value is 2"; //print this.
}
else if(num == 3) //if 'num' value equal to 3 then
{
cout<<"Entered value is 3"; //print this.
}
else //if not mean less then 1 then
{
cout<<"Entered value is Less then 1"; //print this.
}
}
else //if not mean greater then 3 then
{
cout<<"Invalid Entry!"; //print this.
}
getch();
}