Nested Conditions and Guard Clauses
Check consent before checking a seat
A trip organizer first checks for a signed form. Only then should the program report whether a seat is available. A missing form deserves different feedback from a full trip.
A nested condition places a decision inside a selected branch of another decision. The inner check is reached only if execution enters that outer branch.
Trace the path
Identify when the inner decision is reached.
Explain feedback
Preserve distinct messages for distinct failed requirements.
Flatten carefully
Compare nested logic with early-return guard clauses.
An inner decision has an entry condition
Consent check
The form is signed.
trueSeat check
Execution enters the consent branch; no seat is available.
falseFeedback
The inner alternative explains the actual obstacle.
No seat available
Indentation helps readers, braces define JavaScript blocks
In JavaScript, braces define which instructions belong to a branch; indentation makes that relationship visible. The seat condition belongs inside the signed-form branch.
Nesting expresses dependent decisions, but deeply nested logic is hard to scan. A combined condition can summarize eligibility, while a sequence of checks can preserve specific failure messages.
Before starting, review if and alternative branches. These examples use JavaScript; other languages can use different syntax and rules.
Explain which requirement prevented the trip
The form is signed, so the inner seat check runs. No seat is available, so the inner else prints its message.
If signedForm were false, neither seat branch would run. The outer else would print the form request instead.
JavaScript · local teaching example
const signedForm = true;
const seatAvailable = false;
if (signedForm) {
if (seatAvailable) {
console.log('Trip confirmed');
} else {
console.log('No seat available');
}
} else {
console.log('Bring a signed form');
}Expected output
No seat availableReturn early for failed requirements
A guard clause handles an unsuitable case early. Inside a function, return ends that function call, so later checks are not reached. This is not the same as ending the whole application.
The same messages can be preserved with less nesting. This version introduces function parameters and returns; the functions group develops their syntax in detail.
JavaScript · local teaching example
function tripMessage(signedForm, seatAvailable) {
if (!signedForm) return 'Bring a signed form';
if (!seatAvailable) return 'No seat available';
return 'Trip confirmed';
}
console.log(tripMessage(true, false));
console.log(tripMessage(false, true));Expected output
No seat available
Bring a signed formApply the rule to a small change
Use the guard-clause version for a signed form and an available seat. Explain which returns are skipped.
Worked answer
Neither failure guard matches, so execution reaches the final return and produces Trip confirmed. A return is performed only when its condition selects it.
JavaScript · local teaching example
function tripMessage(signedForm, seatAvailable) {
if (!signedForm) return 'Bring a signed form';
if (!seatAvailable) return 'No seat available';
return 'Trip confirmed';
}
console.log(tripMessage(true, true));Expected output
Trip confirmedRecognize mistakes and limits
Replacing both checks with one AND condition can calculate eligibility, but it loses the reason for failure unless you add more logic. Choose structure by the feedback the user needs.
Repeated else branches can be attached to the wrong if when braces are unclear. Keep branches short, name conditions, and trace all combinations of true and false. Avoid introducing functions solely to hide an unclear school policy.
For the language or tool rules used here, see JavaScript control flow.
Check the core idea
JavaScript · code for this question
const signedForm = true;
const seatAvailable = false;
if (signedForm) {
if (seatAvailable) {
console.log('Trip confirmed');
} else {
console.log('No seat available');
}
} else {
console.log('Bring a signed form');
}Apply the idea to a new situation
JavaScript · code for this question
function tripMessage(signedForm, seatAvailable) {
if (!signedForm) return 'Bring a signed form';
if (!seatAvailable) return 'No seat available';
return 'Trip confirmed';
}
console.log(tripMessage(true, false));
console.log(tripMessage(false, true));Preserve the reason while simplifying the path
A nested check runs inside a chosen outer branch. Guard clauses can make failure paths visible early, but simplification should preserve the intended messages and ordering.
Connect this idea with returning values and choosing among named alternatives.
Reaching the recap and completing the quizzes are separate milestones.