Nested If Spot Light
Nested If Spot Light
Figure 4-8
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:
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.
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
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 }
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?