The Basics of ColdFusion Scripting ColdFusion Help

The Cold Fusion scripting language is really an incremental addition to the ColdFusion environment. It Simply serves as a method for bringing familiar ]avaScript-style syntax and pr’ogramming constructs into the ColdFusion environment. This doesn’t mean that
ColdFusion scripting is a subset of ]avaScript, but rather it is designed to be familiar to users of these conventional scripting languages. The Cold Fusion scripting language offers eight script-style statements to the ColdFusion vocabulary:

• If- Else
• While
• Do-While
• For
• Break
• Continue
• For-In
• Switch-Case

Within the ColdFusion scripting environment, no tags are used. Instead, these eight statements can be used in combination with valid ColdFusion expressions (“Writing Expressions”), ColdFusion variables (“Creating and Manipulating Variables”), and ColdFusion functions.

To effectively use ColdFusion scripting, you need to learn the eight statements as well as some basic conventions of syntax.

Understanding Basic Syntax

At the center coldFusion scripting are statements. In its most basic form, a statement is a single line ended by a semicolon. Line breaks are irrelevant. Consider the following simple assignment statement that assigns the string This is a test to the variable Test:

Test – ‘This is a test’;

Because the semicolon delineates the end of the statement, it is possible to use line breaks anywhere. For instance, the following statement is exactly the same as the preceding one


‘This is a test’;

Multiple statements can also be combined in a series to be treated as a single statement. You create the combination by surrounding the series of statements in curly braces·

( Test = ‘This is a test’; AnothenTest – ‘This is another test’; )

This preceding example is a compound statement built out of two other statements. Typically, these compound statements are rewritten so that each individual statement is on its own line:

Test – ‘This is a test’
AnotherTest – ‘This is another test’;

In addition, more complex statement structures can be built out of other statements. You can build these complex structures by using the eight statement constructs outlined earlier. For instance, the If-Else construct takes the form:

If (expression)

In this case, the statements in question can be simple or compound statements. For instance, the following case uses one simple statement and one compound statement:

If (Test is “)
Test = ‘This is a test’;
Else {
Test = ‘This is a test’;
AnotherTest – ‘This is another test’;

Using If-Else Constructs

You have already seen the basic form of the If-Else statement, but let’s review it:

If (expression)

This corresponds to the combination of the CFIF and CFELSEtags that take the following form:

Like the tag-based If construct, the scripted statement does not require the second Else portion. A simple If statement is possible:

If (expression) Statement

Using:For Loops

For loops correspond to the CFLOOPtag when using the FROMand TOattributes. If you need to refresh your memory about the CFLOOPtag, refer “Looping.” Many of the scripting statements are loops, and the concepts will help you understand how the scripted loops work.

The syntax of the For statement is similar tothat used in JavaScript (in contrast, CFLOOPis more familiar to Basic programmers:

For (initial expression; test expression; increment expression)

To understand how this works, let’s look at the relationship between a CFLOOPtag and its corresponding script-based form. Consider the following tag:


This translates into

For (X-I; X LT 10; X – X + 1)

Therefore, the value of INDEXis the variable used in all three expressions of the scripted loop. The initial expression defines the initial starting value for the index variable. The test expression indicates the expression that must be true. for the loop to continue. Finally, the increment expression indicates the change to make to the index variable With,each iteration through the loop.

Therefore, you can adjust the size of the increment through the loop by adjusting the increment expression. For instance, if you want to achieve the same result as using STEP=2in the (FLooP tag, you ,simply use X – X + 2 as the increment expression.

In a For loop, the three expressions can be empty. For instance, the loop re- ( ; ; )

will repeat endlessly Unless part of the statement block of the loop causes the loop to end. You can end the loop by using the Break statement that you will lookat later in this chapter.

Using For-In Loops

A variation on the standard For loop is the For-In loop. The For-In loop is used for looping over a COM collection (see Chapter 30, “Including External Objects”) or a structure (see Chapter 13, “Working with ColdFusion Data Structures”). Recall the use of (FLOOPto loop through a structure:

This corresponds to the following structure in script syntax:

In the preceding loop, during each iteration, the Person variable will be assigned the key for each succeeding entry in the structure.

Using While Loops

You can use the CFLooPtag to create a so-called conditional loop by using the tag in the following form:

<CFLOOPCONDITION-‘ expression’ >

In ColdFusion scripting, this corresponds exactly to the Whi1e loop:
While (expression) Statement;
Written in plain English, these loops translate to: “As long as the expression is true, continue executing the statement.” This means that the statement will never execute if the expression is false when the While statement is first encountered.

Posted on November 16, 2015 in ColdFusion Scripting

Share the Story

Back to Top
Share This