Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
An else-if ladder connects several conditions into one decision. For score = 76, the program checks the thresholds from top to bottom and chooses one output. Each else if continues the same decision, and the final else is the fallback when every earlier condition is false.
int score = 76;
if (score >= 90) {
cout << "A";
} else if (score >= 75) {
cout << "B";
} else if (score >= 60) {
cout << "C";
} else {
cout << "D";
}The braces mark the statements belonging to each branch. The second branch is not a new, independent if statement. It belongs to the same chain as the first if, and the final else belongs to that chain as its last fallback.
Which lines form the single connected decision for score = 76?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
With score = 76, the first test, score >= 90, is false, so its branch is skipped. The next test, score >= 75, is true, so the program prints "B" and immediately leaves the ladder. The later test score >= 60 would also be true, but it is not evaluated because the ladder has already selected a branch.
score = 76
score >= 90 -> false, skip A
score >= 75 -> true, print B
score >= 60 -> skipped
else -> skippedThe conditions are tested from top to bottom, so a broader threshold can take control before a narrower threshold gets a chance. If score >= 60 comes before score >= 75, score = 76 satisfies that first condition and the ladder prints "C". The later score >= 75 condition is skipped even though it is also true.
int score = 76;
if (score >= 90) {
cout << "A";
} else if (score >= 60) {
cout << "C";
} else if (score >= 75) {
cout << "B";
} else {
cout << "D";
}For these thresholds, the useful order is narrowest to broadest: score >= 90, then score >= 75, then score >= 60. A score of 76 fails the first narrow threshold but passes the next one, so it receives B before the broader C condition can be considered.
Place the three threshold conditions in the order that makes score = 76 print "B".
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Separate if statements do not form one connected decision. Each condition is checked independently, so more than one branch can run. With score = 76, both score >= 75 and score >= 60 are true, and the program prints both "B" and "C".
int score = 76;
if (score >= 90) {
cout << "A";
}
if (score >= 75) {
cout << "B";
}
if (score >= 60) {
cout << "C";
}
else {
cout << "D";
}The separate statements produce B and C because each true condition runs its own branch. Use one else-if ladder when the classifications are alternatives and exactly one output should be selected. The difference is not the comparison itself, but whether the conditions are connected into one chain.