User Guide

Table Of Contents
130 Chapter 6: Extending ColdFusion Pages with CFML Scripting
Using switch and case statements
The
switch statement and its dependent case and default statements have the following
syntax:
switch (expression) {
case constant: [case constant:]... statement(s) break;
[case constant: [case constant:]... statement(s) break;]...
[default: statement(s)] }
Use the following rules and recommendations for switch statements:
You cannot mix Boolean and numeric constant values in a switch statement.
Each constant value must be a constant (that is, not a variable, a function, or other expression).
Multiple case constant: statements can precede the statement or statements to execute if any of
the cases are true. This lets you specify several matches for one code block.
No two constant values can be the same.
The statements following the colon in a case statement block do not have to be in braces. If a
constant value equals the
switch expression, ColdFusion executes all statements through the
break statement.
The break statement at the end of the case statement tells ColdFusion to exit the switch
statement. ColdFusion does not generate an error message if you omit a
break statement.
However, if you omit it, ColdFusion executes all the statements in the following case
statement, even if that case is false. In nearly all circumstances, this is not what you want to do.
You can have only one default statement in a switch statement block. ColdFusion executes
the statements in the
default block if none of the case statement constants equals the
expression value.
The default statement does not have to follow all switch statements, but it is good
programming practice to do so. If any
switch statements follow the default statement you
must end the
default block code with a break statement.
The default statement is not required. However, you should use one if the case constants do
not include all possible values of the expression.
The default statement does not have to follow all the case statements; however, it is good
programming practice to put it there.