Control structure: if / else

To print a letter grade, we need to consider 5 possible grades. In this example, we test for each letter grade.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Print the letter grade
#include <iostream>
using namespace std;
int main() { 
  int grade;
   cout<<"Enter your numeric grade:";
  cin>>grade;
  if (grade < 65) cout<<"Your grade is F\n";
  if(grade>=65 && grade<70) cout<<"Your grade is D\n";
  if(grade>=70 && grade<80) cout<<"Your grade is C\n";
  if(grade>=80 && grade<90) cout<<"Your grade is B\n";
  if (grade >= 90) cout<<"Your grade is A\n";
  system("pause");
  return 0;
} //main
CODE

When you test a program like this, you do not need to test every possible grade from 0 to 100, but you should test each Boolean expression. The following values would be good test data: 64,65,69,70,71,79,80,81,90,91.

NEXT: Using nested if/else to find letter grade