Computing & Information Systems    Department Search CIS Site      Tutorials

## Software Design Using C++

### Review of Decision Statements

First, we repeat the description of the problem: Write a C++ function that is given an integer grade (such as 90, 84, 66) and sends back the corresponding letter grade (such as A, B, D) in the function name. Return the letter I for any invalid grade number.

You selected Answer E. This is correct and fairly readable. However, it is not a very efficient method, especially because of the sequence of `if` tests. For example, if NumGrade is 66, it gets compared to 90 and 100, then 80 and 90 again, then 70 and 80, and finally 60 and 70. Surely you can arrange to use fewer tests than this. Go back and see if you can find a better solution.

 `````` /* Given: NumGrade A numeric grade (whole number). Task: To find the corresponding letter grade. Return: This letter grade in the function name. */ char LetterGrade(int NumGrade) { char Ch; Ch = 'I'; // unless we change it below if ((NumGrade >= 90) && (NumGrade <= 100)) Ch = 'A'; if ((NumGrade >= 80) && (NumGrade < 90)) Ch = 'B'; if ((NumGrade >= 70) && (NumGrade < 80)) Ch = 'C'; if ((NumGrade >= 60) && (NumGrade < 70)) Ch = 'D'; if ((NumGrade >= 0) && (NumGrade < 60)) Ch = 'F'; return Ch; } ``````

You can go back to the overall review listing: Review of Introductory Topics.

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: January 15, 2013
Disclaimer