if else statement in C++
It is used when the condition of if statement condition is going to wrong else will print the statement which is defined after the else statement in the program. It is use to save the time as well as to minimize length of program for example when we have two condition then we use two if statement so we achieve this task by using one if and one else statement in program.
Syntax if(condition)
Print Statement 1
else
Print Statement 2
// if condition is wrong than statement 2 will executed
For example int marks=80;
if(marks>33)
cout<<"you are passed in examination"<<endl;
else
cout<<"you are failed in examination"<<endl;
Example 1 #include<iostream.h>
int main()
{
float GPA;
cout<<"Enter your GPA"<<endl;
cin>>GPA;
if(GPA>=2.9)
{
cout<<"you are eligible for Laptop scheme."<<endl;
}
else
cout<<"you are not eligible for Laptop sachem."<<endl;
return 0;
system("pause");
};
Example 2 #include<iostream.h>
int main()
{
float marks;
cout<<"Enter your marks"<<endl;
cin>>marks;
if(marks>33)
cout<<”you are passed in examination ”<<endl;
else
cout<<”you are failed in examination”<<endl;
return 0;
system("pause");
};
Example 3 #include<iostream.h>
int main()
{
int a;
int b;
int c;
cin>>a;
cin>>b;
cin>>c;
if (a>b&&a>c)
cout<<”a is the greatest”<<endl;
if (b>c&&b>a)
cout<<”b is the greatest “<<endl;
if(c>a&&c>b)
cout<<”c is the greatest”<<endl;
else
cout<<”all the number you entered are equall<<endl;
return 0;
system("pause");
};