Nested if Statements

Multiway Selection with else if

Chain else if to pick between three or more paths — only the first true branch runs:

if (distance < 1.0)
{
    zone = "CLOSE";
}
else if (distance < 3.0)
{
    zone = "MID";
}
else
{
    zone = "FAR";
}

A trailing plain else (no condition) catches everything not already matched — it's optional, but leaving it off means "do nothing" is a real possible outcome.

Chained vs. Separate ifs

else if connects the branches together — only one of them ever runs. Writing separate, unconnected if statements instead is a different (and often wrong) thing entirely, because all of them get checked independently:

// Connected — only one branch runs
if (score >= 90) { grade = "A"; }
else if (score >= 80) { grade = "B"; }
else { grade = "C"; }

// NOT connected — multiple could run, overwriting each other
if (score >= 90) { grade = "A"; }
if (score >= 80) { grade = "B"; } // a 95 would hit this too, overwriting "A" with "B"!

The Dangling Else

When ifs are nested without braces, an else always attaches to the closest unmatched if — regardless of indentation:

if (sunny)
    if (hot)
        goToBeach();
else // looks like it belongs to sunny, but it belongs to hot
    bringUmbrella();

This brings an umbrella when it's sunny but not hot — and does nothing at all when it isn't sunny. If that's not what you meant, braces fix it:

if (sunny)
{
    if (hot)
    {
        goToBeach();
    }
}
else // now belongs to the OUTER if, thanks to the braces
{
    bringUmbrella();
}

A Missing else Can Leave a Variable Unassigned

Java requires a local variable (one declared inside a method, like tier below) to be definitely assigned before it's read. If no branch is guaranteed to run, the compiler won't let you read it afterward:

String tier; // local variable, no initial value

if (score >= 8)
{
    tier = "ELITE";
}
// no else — if score < 8, tier is never assigned

System.out.println(tier); // won't compile: tier might not be assigned

Adding a trailing else that assigns tier (or giving tier a starting value) fixes it.

Choosing Between Two Values with ?:

When an if/else exists only to pick one of two values for the same variable, the conditional operator ?: (often called the ternary operator, because it has three parts) does it in a single expression. This if/else:

double speed;
if (boosted)
{
    speed = 1.0;
}
else
{
    speed = 0.5;
}

does the same thing as this one line:

double speed = boosted ? 1.0 : 0.5;

Read it as condition ? valueIfTrue : valueIfFalse: Java checks the condition, then produces exactly one of the two values. Use it for short value choices; keep a regular if/else when a branch needs to do things, not just produce a value.

Common Pitfalls

  • Using separate ifs instead of else if. Separate ifs are each checked independently — a later one can silently overwrite what an earlier one just set.
  • Relying on indentation instead of braces. Java ignores indentation entirely; only { } actually determines which if an else belongs to.
  • Skipping the trailing else when a local variable is only assigned inside branches. The compiler won't let you read a local variable that isn't definitely assigned on every path — add an else, or give the variable a starting value.
  • Stacking ternaries. a ? x : b ? y : z is legal but hard to read — for three or more choices, use an else if chain.

Key Takeaways

  • else if chains pick exactly one branch — the first condition that's true, and nothing after it.
  • Unconnected, separate if statements are checked independently and can overwrite each other's results.
  • An else always attaches to the nearest unmatched if — braces are the only reliable way to control this.
  • condition ? a : b picks one of two values in a single expression — a compact stand-in for an if/else that only assigns.

Derived from csawesome-2026: unit-2-selection-and-iteration/2.04-nested-if-statements.md (CSA 2.4)