The if statement from the previous lesson only handles one path: run some code, or don't. Most real decisions have at least two outcomes โ a user is either logged in or not, a coupon is either valid or expired, a score falls into one grade band or another.
The if...else statement adds that second path, and else if lets you chain as many conditions as you need.
By the end of this lesson, you'll be able to write else and else if branches in both CFScript and Tag Syntax.
Learning Objectives
After completing this lesson, you'll be able to:
- Add an else branch to run code when a condition is false.
- Chain multiple conditions with else if.
- Understand that only one branch in a chain ever runs.
- Order conditions correctly in an else if chain.
The else Branch
else runs when the if condition is false.
Chaining Conditions with else if
Use else if (CFScript) or <cfelseif> (Tag Syntax) to test additional conditions in order. ColdFusion checks each one top to bottom and runs the first branch that matches.
Only the first matching branch runs, even if a later condition would also be true. That's why order matters โ see Common Beginner Mistakes below.
Real-World Example: Login Feedback
Common Beginner Mistakes
Checking Ranges in the Wrong Order
Testing score >= 70 before score >= 90 means a score of 95 incorrectly matches the first branch, since it's also >= 70. When ranges overlap, order the widest match last, or the narrowest condition first.
Using Separate if Statements Instead of else if
Three separate if statements each run their own check even after one has already matched, which wastes work and can produce unexpected results if conditions overlap. Use else if when only one branch should ever run.
Forgetting a Final else
Without a final else, values that don't match any condition simply fall through with no output โ which can look like a bug. Add an else branch to handle unexpected values explicitly.
Best Practices
- Order else if conditions from most specific to least specific for overlapping ranges.
- Use else if instead of independent if statements when only one branch should run.
- Add a final else to handle unexpected values instead of leaving them unhandled.
- Keep each branch focused on one outcome.
Interview Questions
When does the else branch run?
Only when every preceding if / else if condition is false.
In an else if chain, how many branches can run?
At most one โ the first condition that evaluates to true, checked top to bottom.
Why does condition order matter in an else if chain?
Because only the first matching branch runs, a broader condition placed before a narrower one will incorrectly catch values meant for the narrower one.
What is the Tag Syntax equivalent of else if?
<cfelseif>
Summary
In this lesson, you added an else branch to handle the false case of a condition, and chained multiple conditions with else if. You saw how condition order affects overlapping ranges, and practiced both with a grading example and a login-feedback example, in CFScript and Tag Syntax.
What's Next?
The next lesson covers Nested if โ placing an if statement inside another if statement to test additional conditions that only matter once an outer condition is already true.