Handout - Nested-if's, else-if


// This program uses nested-if statements to calculate a letter
// grade from a numeric grade.  The same calculation is then
// made again, using the else-if structure.

#include < iostream>

using namespace std;

int main()
{
  int score;
  cout << "Enter score: ";
  cin >> score;
  cout << "The score of " << score << " earns a grade of " ;

  if (score < 68)
      cout << "NR" << endl;
  else 
      if (score < 78)
          cout << "C" << endl;
      else 
          if (score < 88)
              cout << "B" << endl;
          else
              cout << "A" << endl;

  // the else-if structure is easier to read:

  cout << "Enter score: ";
  cin >> score;
  cout << "The score of " << score << " earns a grade of " ;

  if (score < 68)
    cout << "NR" << endl;
  else if (score < 78)
    cout << "C" << endl;
  else if (score < 88)
    cout << "B" << endl;
  else
    cout << "A" << endl;

  return 0;
}