The following sections describe how to use these CFScript statements:
CFScript assignment statements are the equivalent of the cfset
tag. These statements have the following form:
lval = expression;
lval is any ColdFusion variable reference; for example:
x = "positive"; y = x; a[3]=5; structure.member=10; ArrayCopy=myArray;
You can use ColdFusion function calls, including UDFs, directly in CFScript. For example, the following line is a valid CFScript statement:
StructInsert(employee,"lastname",FORM.lastname);
CFScript includes the following conditional processing statements:
if
and else
statements, which serve the same purpose as the cfif
, cfelseif
, and cfelse
tags
switch
, case
, and default
statements, which are the equivalents of the cfswitch
, cfcase
, and cfdefaultcase
tags
The if
and else
statements have the following syntax:
if(expr) statement [else statement]
In its simplest form, an if statement looks like this:
if(value EQ 2700) message = "You've reached the maximum";
A simple if-else statement looks like the following:
if(score GT 1) result = "positive"; else result = "negative";
CFScript does not include an elseif statement. However, you can use an if
statement immediately after an else
statement to create the equivalent of a cfelseif
tag, as the following example shows:
if(score GT 1) result = "positive"; else if(score EQ 0) result = "zero"; else result = "negative";
As with all conditional processing statements, you can use curly braces to enclose multiple statements for each condition, as follows:
if(score GT 1) { result = "positive"; message = "The result was positive."; } else { result = "negative"; message = "The result was negative."; }
Note: Often, you can make your code clearer by using braces even where they are not required.
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:
switch
statement.
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.
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.
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.
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.
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.
default
statement is not required. However, you should use one if the case
constants do not include all possible values of the expression.
default
statement does not have to follow all the case
statements; however, it is good programming practice to put it there.
The following switch
statement takes the value of a name variable:
switch(name) { case "John": case "Robert": male=True; found=True; break; case "Mary": male=False; found=True; break; default: found=False; } //end switch