DSA FUNDAMENTALS › CONTROL FLOW
An if-else compares a value with a condition and chooses one branch. For score = 73, the condition score >= 90 asks whether 73 is at least 90. The answer is false, so the if branch is skipped and the else branch runs. The two branches are alternatives: exactly one of them runs.
int score = 73;
if (score >= 90) {
cout << "Excellent";
} else {
cout << "Not Excellent";
}A condition is an expression that becomes true or false. A branch is the block of code chosen after that result. The if branch handles true, while the else branch handles false. You can add another condition to the false path by writing else-if, which lets one decision continue into more specific choices.
Given score = 73 and the condition score >= 90, which path runs?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An else-if ladder connects several alternatives into one chain. For score = 73, score >= 90 is false, so the ladder moves to score >= 70. That condition is true, so it prints Good and stops. It does not continue to test score >= 40, even though that comparison would also be true.
if (score >= 90) {
cout << "Excellent";
} else if (score >= 70) {
cout << "Good";
} else if (score >= 40) {
cout << "Pass";
} else {
cout << "Fail";
}Separate if statements do not form one chain. Each separate if checks its own condition, so a score could reach more than one output when conditions overlap. With score = 73, separate tests for score >= 70 and score >= 40 would both be true. An else-if ladder prevents that by making every later test depend on all earlier tests being false.
The order of overlapping conditions changes the result. If score >= 40 appears before score >= 70, score = 73 satisfies the first condition immediately. The ladder prints Pass and never reaches the more specific Good condition. The conditions must therefore go from the highest threshold to the lowest threshold, because the first true condition wins.
if (score >= 40) {
cout << "Pass";
} else if (score >= 70) {
cout << "Good";
} else {
cout << "Fail";
}What does this wrongly ordered ladder output for score = 73?
if (score >= 40) {
cout << "Pass";
} else if (score >= 70) {
cout << "Good";
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The correctly ordered ladder does not need an explicit upper-bound test for Good. Score = 73 fails score >= 90, then passes score >= 70, so it belongs to the range below 90 and at least 70. The ladder itself supplies the upper bound because reaching the second condition proves that the first condition failed.
| CHECK | RESULT FOR SCORE = 73 | EFFECT |
|---|---|---|
| score >= 90 | false | Continue to the next condition |
| score >= 70 | true | Output Good and stop |
| score >= 40 | not checked | Its branch is skipped |
| else | not reached | Handles values failing every condition |
The final else has no comparison of its own. It runs only when score >= 90, score >= 70, and score >= 40 have all failed. That makes Fail the remaining classification. Because the thresholds are ordered from highest to lowest and the ladder stops at the first true condition, score = 73 produces Good once instead of reaching Pass as well.