Multiple else-if clauses allow testing several conditions in sequence. The first true condition's block executes, and remaining conditions are skipped. This avoids deeply nested if-else structures and improves readability.
Write a multi-way branching program using else-if chains. Compare readability to nested if-else.
From conditional statements, you know that `if-else` lets your program choose between two paths based on whether a condition is true or false. But many real situations have more than two outcomes. A grading system does not just say "pass or fail" — it assigns A, B, C, D, or F. A weather app does not just say "raining or not" — it might report sunny, cloudy, rainy, or snowy. Else-if chains extend the basic `if-else` to handle multiple mutually exclusive conditions in sequence.
The structure looks like this: `if (condition1) { ... } else if (condition2) { ... } else if (condition3) { ... } else { ... }`. The program tests each condition in order, top to bottom. The first condition that evaluates to true has its block executed, and then the entire chain is done — no further conditions are checked. The final `else` (optional) catches anything that did not match above. Think of it like a bouncer checking a list of criteria: "Are you on the VIP list? No? Are you on the guest list? No? Are you a member? No? Sorry, you can't come in."
Order matters, and this is the most important thing to internalize. Consider a grading example: if you write `if (score >= 60)` before `if (score >= 90)`, a student with a score of 95 matches the first condition (`95 >= 60` is true) and gets the grade meant for a D, never reaching the A condition. The correct order is to test the most specific or restrictive condition first: `score >= 90`, then `score >= 80`, then `score >= 70`, and so on. This is called testing from most restrictive to least restrictive, and getting it wrong is one of the most common bugs in beginner code.
Else-if chains also improve readability compared to deeply nested `if-else` structures. Without else-if, handling four cases requires nesting three levels deep — each `else` contains another `if-else` inside it, creating a staircase of indentation. An else-if chain keeps everything at the same indentation level, making the logic immediately visible. When you find yourself nesting `if` inside `else` inside `else`, that is usually a signal to refactor into an else-if chain. Later, you will learn about `switch` statements, which handle a related pattern — choosing between many possible values of a single expression — but else-if chains remain the right tool when conditions involve different variables or complex boolean expressions.