This module covered five ways to make a decision in ColdFusion: if, if...else, nested if, switch, and the ternary operator. Like loops, they can often be forced to solve the same problem โ the real question is which one makes the decision easiest to read at a glance.
The deciding factor comes down to two things: how many outcomes are you choosing between, and are you comparing one value against several exact options, or evaluating a range or combination of conditions?
Learning Objectives
After completing this lesson, you'll be able to:
- Match a real situation to the conditional construct that fits it best.
- Explain why switch is a better fit than a long if / else if chain in some cases, and worse in others.
- Recognize when a ternary makes code clearer, and when it makes code harder to read.
The Decision
Decision Table
| Situation | Best choice | Why |
|---|---|---|
| Run code only if one condition is true, otherwise do nothing | if | Simplest option โ no else branch to manage |
| Exactly two outcomes โ one or the other, always | if...else | Guarantees one of exactly two paths always runs |
| Several outcomes based on ranges or unrelated conditions | Nested if / else if chain | Each branch can test something completely different |
| One variable compared against several exact, known values | switch | Reads as a clean list of exact matches, not a chain of comparisons |
| A range check (age >= 18) or comparing two different variables | if / else if | switch can only test exact equality on one expression |
| Assigning one of two values to a variable, based on a simple condition | Ternary operator | One line instead of a four-line if...else just to set a variable |
| The logic inside each branch is more than a single simple statement | if / else if / switch | Cramming multi-step logic into a ternary makes it hard to read |
if vs. switch: The Most Common Mix-Up
switch only tests one expression for exact equality against a fixed set of values โ it can't evaluate a range like age >= 18, and it can't check two unrelated variables in the same case. If either of those is true, reach for if / else if instead, even if you're comparing the same variable in every branch.
<cfscript>
// This does NOT work โ switch can't evaluate ranges
switch (true) {
case (age >= 18):
writeOutput("Adult");
break;
}
</cfscript>Testing switch (true) against boolean case expressions technically runs in some engines, but it defeats the entire point of switch and is far less readable than if / else if โ avoid it.
When a Ternary Helps, and When It Hurts
<cfscript> status = isActive ? "Active" : "Inactive"; </cfscript>
<cfscript>
if (orderTotal >= 500) {
shippingCost = 0;
applyLoyaltyPoints(order.id);
} else {
shippingCost = 50;
}
</cfscript>The ternary works well for a single value assignment. The moment a branch needs more than one statement, a ternary can't express it cleanly โ use if...else instead.
Common Beginner Mistakes
Reaching for switch when the comparison isn't exact equality
switch can't evaluate age >= 18 or a check across two different variables โ those need if / else if instead.
Using nested if where else if would be flatter and clearer
Stacking if inside if inside if for conditions that are really just alternatives to each other reads harder than a single if / else if chain covering the same logic.
Cramming multi-step logic into a ternary
result = x > 10 ? (doSomething(), doSomethingElse()) : y forces multiple actions into an expression meant for a single value โ use a normal if...else instead once a branch needs more than one statement.
Best Practices
- Ask "am I comparing one thing to several exact values?" โ if yes, consider switch; if no, use if / else if.
- Reserve the ternary for simple value assignment, not for triggering multiple actions.
- Flatten nested if into else if whenever the branches are really alternatives to each other, not conditions layered inside one another.
- Always include a default (switch) or a final else (if chains) to handle the case nothing else matched.
Interview Questions
Why can't switch replace an if chain that checks a range?
switch only tests one expression for exact equality against a fixed list of values โ it has no way to express a comparison like age >= 18.
When should you use a ternary instead of if...else?
Only when you're assigning one of two values to a variable based on a simple condition โ once a branch needs more than one statement, use if...else instead.
When is switch a better choice than an if / else if chain?
When you're comparing a single variable against several exact, known values โ switch reads as a clean list of matches instead of a chain of == comparisons.
Summary
There's rarely one single "correct" way to write a conditional โ several will usually work. The one worth picking is whichever one matches what you're actually comparing: a simple yes/no uses if, exact-value matching uses switch, ranges and combined conditions use if / else if, and simple value assignment uses the ternary.
This closes out the Decision Making module.
What's Next?
The next module covers Loops โ starting with the for loop, which repeats a block of code a set number of times.