The basic syntax for the if-else statement is
if(condition)
{
//Statements to be executed are here in if’s
body
}
//if the above condition is not true
else if(condition)
{
//Statements to be executed are here in else if’s
body
}
else
{
//default statement if no condition is true
}
if-else Sample Program
#include <iostream>
using namespace std;
int main(){
int marks;
cout << "What mark did you get in the test?" << endl;
cin >> marks;
if(marks >= 90)
{
cout << "You got an A+" << endl;
cout << "You Passed!" << endl;
}
else if(marks >= 80)
{
cout << "You got an A" << endl;
cout << "You Passed!" << endl;
}
else if(marks >= 70)
{
cout << "You got an B" << endl;
cout << "You Passed!" << endl;
}
else if(marks >= 60)
{
cout << "You got an C" << endl;
cout << "You Passed!" << endl;
}
else if(marks >= 50)
{
cout << "You got an D" << endl;
cout << "You Failed!" << endl;
}
else if(marks >= 40)
{
cout << "You got an E" << endl;
cout << "You Failed!" << endl;
}
else
{
cout << "You got an F" << endl;
cout << "You Failed!" << endl;
}
return 0;
}
Post a Comment