DSA SheetLesson · no judge

DSA FUNDAMENTALSCONTROL FLOW

If-Else and Else-If Ladders Choose One Classification

Reading · 5 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 14

An if-else chooses one of two paths

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.

CPPThe false result for score = 73 selects the else branch.
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.

CHECKPOINT 1Not answered

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 stops at the first true condition

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.

CPPThe ladder selects Good and stops after score >= 70 becomes 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 path of score 73 through the classification ladderA flowchart starts with score = 73. The test score >= 90 is false, so the path continues to score >= 70. That test is true, so the path outputs "Good" and stops. The later test score >= 40, its "Pass" output, and the final "Fail" output are shown but are not reached.score >= 90FALSE for 73score >= 70TRUE for 73STOPscore >= 40NOT REACHEDscore = 73ExcellentGoodPassFailtrue · not takenfalsetrue · selectedfalse · not reachedtrue · not reachedfalse · not reached73 stops at the first true condition: Good
The first true condition ends the ladder.

Broad conditions placed first can hide the correct branch

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.

CPPThis order is wrong for the intended classifications: the broad first condition captures score = 73.
if (score >= 40) {
    cout << "Pass";
} else if (score >= 70) {
    cout << "Good";
} else {
    cout << "Fail";
}
CHECKPOINT 2Not answered

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 comparison operators give score 73 exactly one classification

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.

CHECKRESULT FOR SCORE = 73EFFECT
score >= 90falseContinue to the next condition
score >= 70trueOutput Good and stop
score >= 40not checkedIts branch is skipped
elsenot reachedHandles values failing every condition
Each comparison guides score = 73 to one branch.

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.

Next part · Quiz