If..else statement is used when different block of statement is to be executed on condition true and false.
Syntax :
if(condition) { block 1 of statement; } else { block 2 of statement; }
if condition is true then block 1 statement will be executed and if condition is false block 2 statement will execute.
Example :
#include <iostream>
using namespace std;
int main()
{
int number = 0;
cout<<"\nEnter an integer : ";
cin>>number;
if (number > 10)
{
cout<<"\nYou entered "<<number<<" which is greater than 10\n";
}
else
{
cout<<"\nYou entered "<<number<<" which is less than 10\n";
}
return 0;
}
Output :
Enter an integer : 8 You entered 8 which is less than 10
0 Comments