Using Do-While Loops ColdFusion Help

Do-Whi1e loops do not have a direct correlation in the CFLOOPtag syntax. Rather, they, are a slight variation on the regular Whi1e loop. In many programming languages, While loops are known as Repeat-Unti 1 loops. The main difference between the While and the Whi1e loop is that the condition is moved to the end of the loop:

Do
Statement;
While (expression)

Translated  to plain English, this becomes: “Continue to execute the statement until the express! n is false.” This is subtly different from the defirution of the Whi1e loop but it is an important difference: Because the Do-Wh;1e loop tests the expression at the end of the loop, even if the expression is false when the loop is first encountered, the statement will execute at least once.

Using Break and Continue

Sometimes, it is necessary to prematurely end a loop. This can be done with the Break statement. Used in the statement block of a loop, it causes the loop to end regardless of the . value of the index variable or the condition that normally regulates the flow of the loop. The Conti nue statement1is similar to Break. The Conti nue statement, when used in the statement block of a loop, causes the current iteration of the loop to end at the Continue statement and the loop to move on to its next iteration.

Using Switch-Case

The final script statement to consider is the Sw; ch-Case statement. This corresponds to the use of CFSWITCH and CFCASE. The syntax of the Switch-Case statement is:

commenting Your Code

Just as with HTML and regular tag-based ColdFusion code, it is a good idea to add comments to your code: Comments are segments of code that are not executed and can contain descriptive statements regarding the logic and purposes of your program code. ColdFusion scripting supports two types of comments that use the same syntax as JavaScript comments.
One-line comments start with two slashes  and continue to the end of the line:

// This is a comment

Comments do not need to start at the beginning of the line. Instead, they can start after program code on, the line:

Statement:// This is a comment

This type of one-line comment is useful for placing short descriptions of code on the same line as the code itself. For instance, if you are assigning a value to a new variable, you can use a one-line comment to quickly describe the variable:

One-line comments are also useful for quickly commenting out a single line of code when debugging your scripts.

In addition to these single-line-comments, ColdFusion scripts support JavaScriptstyle multi-line comments. Multi-line comments start with a slash followed by an asterisk ( /*) and continue until the first occurrence of an asterisk followed by a slash ( */). This is similar in concept to regular ColdFusion tag comments, which start with <! — and continue until the first occurrence of —>.

Posted on November 16, 2015 in ColdFusion Scripting

Share the Story

Back to Top
Share This