All lessonsOpen Open Open Open Open
3. Decisions
else if and nested decisions
0 of 5 activities0%
Reading 1
Chains of else if
When you have three or more branches, chain else if. Only the first true condition runs; the rest are skipped.
You can also nest an if inside another if or else. That is useful when a second check only matters after the first one passes.
Keep nesting shallow when you can — long else if chains are often clearer than deep nesting.
if (score >= 90) {
cout << "A" << endl;
} else if (score >= 80) {
cout << "B" << endl;
} else if (score >= 70) {
cout << "C" << endl;
} else {
cout << "below C" << endl;
}Check 2
Which branch runs
score is 85. Which message prints in the A/B/C chain above?
Fill in 3
Middle branch
Try it 4
Grade bands
Sample input is 72. Run it, then try 95 and 60.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Sign of a number
Read one integer. Print positive if it is greater than 0, zero if it is 0, and negative if it is less than 0. Exact words, one line.
main.cpp
Loading editor…