C++ if-else-if ladder is used to work on multiple conditions.Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.The else if condition is checked only if all the conditions before it (in previous else if constructs, and the parent if constructs) have been tested to false.
Q.) W.A.P to print the greater number using simple if-else-if ladder statement. ?
#include
using namespace std;
int main(){
int score;
cout << "Enter your score between 0-100\n";
cin >> score;
/* Using if else ladder statement to print
Grade of a Student */
if(score >= 90){
// Marks between 90-100
cout << "YOUR GRADE : A\n";
} else if (score >= 70 && score < 90){
// Marks between 70-89
cout << "YOUR GRADE : B\n";
} else if (score >= 50 && score < 70){
// Marks between 50-69
cout << "YOUR GRADE : C\n";
} else {
// Marks less than 50
// if none of the conditions is true
cout << "YOUR GRADE : Failed\n";
}
return 0;
}
Output
Enter your score between 0-100
72
YOUR GRADE : B
Enter your score between 0-100
10
YOUR GRADE : Failed