0% found this document useful (0 votes)
10 views3 pages

Nested If Spot Light

The document explains the use of nested if statements to evaluate multiple conditions in programming, specifically for determining letter grades based on numeric test scores. It provides an example algorithm and code that assigns grades (A, B, C, D, F) based on a 10-point grading scale. The structure of the nested decision-making process is illustrated through a flowchart and a complete program code snippet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Nested If Spot Light

The document explains the use of nested if statements to evaluate multiple conditions in programming, specifically for determining letter grades based on numeric test scores. It provides an example algorithm and code that assigns grades (A, B, C, D, F) based on a 10-point grading scale. The structure of the nested decision-making process is illustrated through a flowchart and a complete program code snippet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

5 Nested if Statements 173

Figure 4-8

Testing a Series of Conditions


In the previous example you saw how a program can use nested decision structures to test
more than one condition. It is not uncommon for a program to have a series of conditions to
test and then perform an action depending on which condition is true. One way to accom-
plish this is to have a decision structure with numerous other decision structures nested inside
it. For example, consider the program presented in the following In the Spotlight section.

In the Spotlight:
Multiple Nested Decision Structures
Dr. Suarez teaches a literature class and uses the following 10-point grading scale for all of
his exams:

Test Score Grade


90 and above A
80–89 B
70–79 C
60–69 D
Below 60 F
He has asked you to write a program that will allow a student to enter a test score and then
display the grade for that score. Here is the algorithm that you will use:
Ask the user to enter a test score.
Determine the grade in the following manner:
If the score is greater than or equal to 90, then the grade is A.
Otherwise, if the score is greater than or equal to 80, then the grade is B.
Otherwise, if the score is greater than or equal to 70, then the grade is C.
Otherwise, if the score is greater than or equal to 60, then the grade is D.
Otherwise, the grade is F.
174 Chapter 4 Making Decisions

You decide that the process of determining the grade will require several nested decisions
structures, as shown in Figure 4-9. Program 4-12 shows the code for the complete program.
The code for the nested decision structures is in lines 17 through 45.

Figure 4-9 Nested decision structure to determine a grade

False True
score
>= 90
Display "Your
False True grade is A."
score
>= 80
Display "Your
False True grade is B."
score
>= 70
Display "Your
False True grade is C."
score
>= 60
Display "Your Display "Your
grade is F." grade is D."

Program 4-12

1 // This program uses nested if/else statements to assign a


2 // letter grade (A, B, C, D, or F) to a numeric test score.
3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8 // Constants for grade thresholds
9 const int A_SCORE = 90,
10 B_SCORE = 80,
11 C_SCORE = 70,
12 D_SCORE = 60;
13
14 int testScore; // To hold a numeric test score
15
16 // Get the numeric test score.
17 cout << "Enter your numeric test score and I will\n";
4.5 Nested if Statements 175

18 cout << "tell you the letter grade you earned: ";
19 cin >> testScore;
20
21 // Determine the letter grade.
22 if (testScore >= A_SCORE)
23 {
24 cout << "Your grade is A.\n";
25 }
26 else
27 {
28 if (testScore >= B_SCORE)
29 {
30 cout << "Your grade is B.\n";
31 }
32 else
33 {
34 if (testScore >= C_SCORE)
35 {
36 cout << "Your grade is C.\n";
37 }
38 else
39 {
40 if (testScore >= D_SCORE)
41 {
42 cout << "Your grade is D.\n";
43 }
44 else
45 {
46 cout << "Your grade is F.\n";
47 }
48 }
49 }
50 }
51
52 return 0;
53 }

Program Output with Example Input Shown in Bold


Enter your numeric test score and I will
tell you the letter grade you earned: 78 [Enter]
Your grade is C.

Program Output with Different Example Input Shown in Bold


Enter your numeric test score and I will
tell you the letter grade you earned: 84 [Enter]
Your grade is B.

Checkpoint
4.15 If you executed the following code, what would it display if the user enters 5?
What if the user enters 15? What if the user enters 30? What if the user enters −1?

You might also like