CFScript Statements
CFScript
contains a number of statements to handle flow control and looping,
as we’ll discuss in this section. Note that you can’t use
any CFML tags within a CFSCRIPT
block.
if/else
The
if
/else
statement has the same
functionality as the CFIF
and
CFELSE
(and by extension,
CFELSEIF
) tags in regular CFML. The
if
statement can be used alone, as in:
if (expression
)statement
;
Or it can be used with the else
statement, as in:
if(expression
)statement
; elsestatement
;
You can also use the else
and
if
statements in combination to achieve the same
results as the CFELSEIF
tag:
if (expression
)statement
; else if (expression
) statement; elsestatement
;
To understand the differences between the
CFIF
/CFELSE
tags and the
if
/else
statement, let’s
first consider an example that uses the CFIF
and
CFELSE
tags. Example 18-3 assigns
the current year to a variable called TheYear
. The
CFIF
and CFELSE
tags are then
used to determine whether the current year is a leap year. The
results are then written to the browser.
Example 18-3. Determining if the Current Year Is a Leap Year Using CFIF/CFELSE
<!--- set a variable equal to the current year ---> <CFSET TheYear=Year(Now( ))> <!--- Check to see if the current year is a leap year. ---> <CFIF IsLeapYear(TheYear)> <CFSET ReturnOutput="is a leap year."> <CFELSE> <CFSET ReturnOutput="is not a leap year."> </CFIF> <CFOUTPUT> #TheYear# #ReturnOutput# </CFOUTPUT>
The same code can easily be rewritten using CFScript. Example 18-4 shows the ported ...
Get Programming ColdFusion now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.