Handling Many Cases with else if
Introduction
In the last lesson, we used if and else to choose between two outcomes. Real programs often need more choices: a stock count might be empty, critically low, running low, or healthy. A flat chain lets us list those options clearly.
The chain is still one decision. We do not want the program to print both “critically low” and “running low” for the same count, even if both comparisons are true. Instead, JavaScript treats the branches as alternatives and stops once it finds a match. Keeping the conditions in a single chain makes that rule visible as we read the code.
We will add else if branches between if and else. JavaScript checks the chain from top to bottom, runs the first branch whose condition is true, and skips the rest. We will also see why narrow conditions often need to come before broader ones.
Add Another Condition with else if
An else if gives a chain another condition to check when the earlier condition was false. Consider the stock count below:
The first condition is false, because four is not zero. The next condition is true, so JavaScript prints Critically low. and stops checking this chain:
Only one branch in a chain runs. Even though stockCount < 20 is also true for four, JavaScript has already found a true condition earlier in the chain. The else at the end has no condition; it catches every value that did not match above.
The same tracing method works with any stored value: write down the value, test the first condition, and move to the next only if the answer is false. A value of zero immediately matches the first condition and never reaches the other comparisons. A value of twenty-five fails all three comparisons and arrives at the final else. A short trace often explains an output more clearly than rereading the whole chain at once.
Check the Branches from Top to Bottom
We can trace the chain before running it. For a count of 12, stockCount === 0 is false. Then stockCount < 5 is false. Then stockCount < 20 is true, so the running-low message appears. JavaScript does not continue to the final else:
Running the code confirms our trace:
Reading a chain this way is useful when an output surprises us. Start at the first condition, evaluate it using the current values, and move down only if it is false. Stop at the first true condition.
The order also makes the meaning of a chain easier to communicate. We can start with exceptional cases such as zero, follow with a narrow range that needs special attention, and finish with broader ranges. A condition does not have to cover every possible number by itself; the later branches fill the remaining cases. What matters is that each branch is reached only after earlier questions have failed.
Why Order Matters
The order of conditions can make a later branch impossible to reach. Imagine a cafe counting cups left for a lunch rush. A broad warning at fewer than ten cups must not hide the urgent warning at fewer than three:
This order prints the broad warning even though only two cups remain:
The first condition is true, so JavaScript never checks the later, more specific one. To make the urgent case reachable, we check remainingCups < 3 first and remainingCups < 10 after it. Any value below three is also below ten. The following practice uses different stock thresholds and asks us to repair the same ordering error.
A Final else Catches the Rest
Without a final else, the program might print nothing if every condition is false. A final branch gives the chain a result for every possible value:
The count is not zero, not below five, and not below twenty, so the final branch runs:
The final else makes the result complete. It does not need a condition because JavaScript reaches it only after every earlier condition was false.
We can change the stored count and predict a different branch without changing the chain itself. Try 0, 3, 12, and 25: the first three values each match a different condition, while the last value reaches the final branch. Separating data from choices lets the same decision rules classify many values; changing one declaration lets us explore those cases safely.
The Full Program
We can check the chain without copying the entire program again. The stock report above has four outcomes, and each test stops at the first match:
stockCount | First matching branch | Message |
|---|---|---|
0 | stockCount === 0 | Sold out. |
4 | stockCount < 5 | Critically low. |
12 | stockCount < 20 | Running low. |
25 | final else | Stock level is healthy. |
For each row, every earlier condition is false. We can use this compact trace to check both the order and the boundary values before writing another chain. A single chain prints one message, even when a later condition would also be true.
Conclusion and Next Steps
We have extended a two-way choice into an if / else if / else chain. JavaScript checks conditions from top to bottom and runs only the first branch that matches. A final else catches the values left over, and putting narrow conditions before broad ones keeps the intended cases reachable.
Next, we will make JavaScript repeat a block with a counting for loop. We will read each part of its header and use the counter inside the repeated block.
