DevLearningTools

๐Ÿšง This site is under active construction โ€” new tools, guides, and pages are added every week.

MODULE 3 ยท LESSON 04

switch

Comparing one variable against many discrete values in ColdFusion with the switch statement, in both CFScript and Tag Syntax (cfswitch / cfcase / cfdefaultcase).

New lessons are added one at a time as the course gets built out โ€” a graded quiz for each lesson is still on the way.

A long chain of else if statements that all compare the same variable to different exact values gets repetitive fast. When you're matching one variable against many discrete possibilities โ€” a day of the week, a status code, a menu choice โ€” the switch statement is a cleaner fit.

By the end of this lesson, you'll know how to write a switch statement in both CFScript and Tag Syntax, group multiple values into one case, and provide a default for unmatched values.

Learning Objectives

After completing this lesson, you'll be able to:

  • Write a switch statement in CFScript and Tag Syntax.
  • Match multiple values to a single case.
  • Provide a default case for unmatched values.
  • Understand why break matters in CFScript.
  • Know when switch is a better fit than else if.

Basic switch Statement

switch compares one expression against a list of case values and runs the matching branch.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

<cfdefaultcase> can technically appear anywhere inside <cfswitch>, but convention โ€” and readability โ€” puts it last.

Why break Matters

In CFScript, without break, execution falls through into the next case even if it doesn't match. Tag Syntax doesn't have this problem โ€” each <cfcase> is self-contained and doesn't need a break.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

Without break, matching "B" also runs the "C" and default branches โ€” every case after the match executes until a break is hit. Always add break unless you intend to fall through.

Matching Multiple Values to One Case

Tag Syntax accepts a comma-separated list in a single <cfcase>. CFScript achieves the same result by stacking case labels with no code between them, so they share one block.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

String matching in switch is case-insensitive by default, in both syntaxes โ€” case "sat" would match day = "Sat" too, the same way ColdFusion's == and EQ ignore case for strings.

Case Values Are Usually Constants

Traditionally, each case value has to be a fixed constant, not a variable โ€” switch(status) { case userStatus: ... } wasn't allowed, because ColdFusion needed to know every case value in advance. ColdFusion 2021 and Lucee both lifted this restriction, so a case value can now be a variable, function call, or expression evaluated at runtime.

If you're not sure which ColdFusion version a project targets, stick to constant case values for the widest compatibility.

Common Beginner Mistakes

Forgetting break in CFScript

Without break, execution falls through into every following case regardless of whether it matches, producing output from multiple branches instead of one.

Using switch for Ranges

switch only matches exact values โ€” it can't test score >= 80. Use if / else if for ranges and comparisons; use switch for exact matches like status codes or fixed categories.

Forgetting a default Case

Without default (or <cfdefaultcase>), a value that matches nothing simply produces no output, which can look like a silent bug. Always include a default for unexpected values.

Best Practices

  • Use switch when comparing one variable against several exact, known values.
  • Use if / else if instead of switch for ranges or multi-variable conditions.
  • Always include break in every CFScript case unless you intend to fall through.
  • Always include a default / <cfdefaultcase> to handle unexpected values.

Interview Questions

What does switch compare against each case?

A single expression's value, matched exactly against each case's value.

What happens if you forget break in a CFScript switch?

Execution falls through into the following case blocks until a break is reached or the switch ends.

How do you match multiple values to one case in Tag Syntax?

List them comma-separated in a single <cfcase value="a,b"> tag.

When is switch not a good fit?

When you need to test ranges or comparisons rather than exact values โ€” use if / else if instead.

Summary

In this lesson, you learned how to replace a long else if chain with a switch statement, saw why break is required in CFScript to avoid fallthrough, grouped multiple values into a single case, and added a default branch for unmatched values โ€” in CFScript and Tag Syntax.

What's Next?

The next lesson covers the ternary operator โ€” a compact shorthand for simple if...else assignments.