Conditional Statements: Choose a Branch
Give different feedback for different marks
A teacher wants to print “Excellent” for marks of at least 40, “Passed” for marks of at least 25, and “Try again” for lower marks. Only one message should appear for each student.
A conditional statement chooses which instructions to execute based on a condition. In an if/else-if chain, the first true condition selects its branch and the remaining alternatives are skipped.
Follow a branch
Trace which condition is tested and which instructions run.
Order thresholds
Place more demanding thresholds before broader ones.
Test boundaries
Check values exactly at 25 and 40 as well as below them.
Trace a mark of 35
First condition
35 is not at least 40.
falseSecond condition
35 is at least 25.
trueSelected branch
Print the passing feedback and skip the final alternative.
Passed
A condition produces an answer; a branch contains work
JavaScript if places a condition in parentheses and its instructions in braces. else if supplies another condition, while else is the fallback when none of the earlier conditions matched.
Conditions are checked in order. A mark of 45 satisfies both numeric thresholds, but only the first matching branch executes in this chain. Put the narrower high-mark condition first.
Before starting, review inclusive comparisons. These examples use JavaScript; other languages can use different syntax and rules.
Choose exactly one feedback message
Mark 35 fails the first comparison, then passes the second. The program prints Passed, not Try again.
The example assumes a previously validated numeric mark from 0 to 50. Choosing feedback does not validate that input automatically.
JavaScript · local teaching example
const mark = 35;
if (mark >= 40) {
console.log('Excellent');
} else if (mark >= 25) {
console.log('Passed');
} else {
console.log('Try again');
}Expected output
PassedSeparate if statements can both execute
Independent if statements ask separate questions. Here 45 meets both thresholds, so both messages appear.
Use separate conditions when both actions are intended; use an alternative chain when the task requires exactly one classification.
JavaScript · local teaching example
const mark = 45;
if (mark >= 40) { console.log('Excellent'); }
if (mark >= 25) { console.log('Passed'); }Expected output
Excellent
PassedApply the rule to a small change
Change the mark to exactly 40 and predict the single message. Keep the same threshold order.
Worked answer
Forty meets the first inclusive condition, so Excellent is printed. The passing branch is skipped even though 40 is also at least 25.
JavaScript · local teaching example
const mark = 40;
if (mark >= 40) {
console.log('Excellent');
} else if (mark >= 25) {
console.log('Passed');
} else {
console.log('Try again');
}Expected output
ExcellentRecognize mistakes and limits
Putting mark >= 25 before mark >= 40 would classify every excellent mark as Passed. Trace overlapping conditions before relying on a chain. A valid program can therefore implement the wrong school rule.
Use === to compare values rather than = to assign them. Braces make the controlled instructions clear. Test zero, 24, 25, 39, 40 and 50, plus invalid input at the validation boundary.
For the language or tool rules used here, see JavaScript control flow.
Check the core idea
JavaScript · code for this question
const mark = 35;
if (mark >= 40) {
console.log('Excellent');
} else if (mark >= 25) {
console.log('Passed');
} else {
console.log('Try again');
}Apply the idea to a new situation
Trace the first matching condition
A conditional changes execution, not the meaning of input data. Order overlapping thresholds, distinguish alternative branches from independent checks, and test exact boundaries.
Connect this idea with decisions inside decisions and combined conditions.
Reaching the recap and completing the quizzes are separate milestones.