Control structure: if / else

Sometimes we need to nest successive if/else statements:

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

This is a more efficient program. Once one of the Boolean expressions is true, all of the nested else statements are skipped.

NEXT: Use a variable for grade