Home » Tutorials » ColdFusion » Loop Statements

Loop Statements

In ColdFusion, loops are also quite a bit different from other languages in that their arguments look more like html attributes. However, I must first start out that telling you ColdFusion loops are one of my favorite things about ColdFusion. You can literally loop through anything in about any way that you want. You can loop through query results by just giving the loop the query name and a variable to increment. Alternatively, you can simply loop within ranges by giving ColdFusion the increment variable, starting point, ending point, and an index. Granted it isn’t as cool as ColdFusion being able to query a query (we will get to this), but the flexibility of loops give ColdFusion a step up over other languages.

Example
<cfoutput>
    <cfloop index="x" from="1" to="10" step="1">
        #x# <br/>
    </cfloop>
</cfoutput>

The index argument is the variable you want to be involved in the loop. Usually, we will increment this variable by the step argument until we have ColdFusion return all the results that we wanted. from is your beginning value of x, and to is the finally result of x. The last argument below is the step, which is by how much do we want to increment the index.

Looping Query Results

Looping query results requires that you already know how to use ColdFusion to get results from an SQL server. What you need to do is to run a query first. Then that query will be an object that you can loop through. You just need to provide the query name and then you need some way to use each iteration to output a different record of the query. You can simply use an incrementing variable that can be defined in the initial loop statement. It is best to discuss this when we have an actual query to run.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Loop Statements". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/coldfusion/loop-statements/.

  • Stewart, Suzy. "Loop Statements". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/coldfusion/loop-statements/. Accessed 16 March, 2024.

  • Stewart, Suzy. Loop Statements. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/coldfusion/loop-statements/.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.