ColdFusion
  Home arrow ColdFusion arrow Page 4 - What You Must Know About ColdFusion Flow-C...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
COLDFUSION

What You Must Know About ColdFusion Flow-Controls
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2005-09-14

    Table of Contents:
  • What You Must Know About ColdFusion Flow-Controls
  • What if, and where to scenarios
  • Which way to go?
  • Keep looking until you find
  • Breaking out of loops
  • Call a halt and end

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    What You Must Know About ColdFusion Flow-Controls - Keep looking until you find


    (Page 4 of 6 )

    Searching through a group of objects until you find the desired object is a commonplace action. In programming this is achieved by resorting to the technique called loop. In this technique you go through the group of objects one at a time, repeatedly displaying what you find until such time as you have exhausted all the items, or found what you wanted. You could also use this technique to count objects. The loop construct is implemented by the <cfloop.attributes.../> construct

    The CFML's <cfloop> tag

    ColdFusion documents three kinds of loops. The attributes of the <cfloop.../> determine which kind of loop is implemented. This is a list of attributes (parameters) that are available for <cfloop.../>. We will be discussing in this tutorial only the index and conditional loops and the 'looping over collection' will be postponed until queries, structures, and so on are discussed.

    • Index Loop (parameters: index, from, to, step)
    • Conditional Loop (parameter: condition)
    • Looping over a Collection[/Query/structure/list] (parameter: depends on sub-types)
    Index Loop

    The processing is performed from the beginning value of the index to the end value of the index, incrementing (or advancing) the process by the step (by the increment). The processing ends after reaching the end value of the idnex.

    Syntax of an Index Loop
    <cfloop index = "parameter name"
    from = "beginning value"
    to = "ending value"
    step = "increment">
    ...
    Process the items here
    ...
    </cfloop> 
    Example 9

    The range of the index is from 1 to 5 including 1 and 5. It starts with 1 goes into the block where it encounters a text followed by the variable which has the index value(1) at that instant and after this it is automatically incremented by 1(1 is default). This is repeated till it's value is equal to 5.

    <cfloop index = "Count" from = "1" to = "5">
    <cfoutput>You are now at: #Count#.
    </cfoutput><br>
    </cfloop>
    

    The displayed output when this code runs is:

    You are now at: 1.
    You are now at: 2.
    You are now at: 3.
    You are now at: 4.
    You are now at: 5.

    The step value can be chosen as necessary, and so are the values of from and to. It is easy to see that you could count down, as well, using a negative step and a higher from value, and the displayed output when you run the code is the reverse of the previous example.

    <cfloop index = "Count" from = "5" to = "1" step=-1>
    <cfoutput>You are now at: #Count#.
    </cfoutput><br>
    </cfloop>
    

    While looping, you could also embed further conditional statements so that you get a more focused output. Review the following code. You will see that although the looping is effective, the output is written out only when the count exceeds the value 3, that is for index 4 and index 5.

    <cfloop index = "Count" from = "1" to = "5" >
    <cfif #Count# gt 3>
    <cfoutput>You are now at: #Count#.
    </cfoutput>
    </cfif>
    <br>
    </cfloop>
    Conditional Loop
    

    In this case, the loop iteration (that means the act of going through the loop) is carried out until such time as a certain condition is attained. In the previous case of Index loop we saw attributes like from, to, index and step. In this case the attribute is condition. The syntax is as follows;

    <cfloop    condition = "expression">
    ...Process this as long as condition is satisfied..
    </cfloop>
    
    Example 10

    We start off with the variable vol having a initial value of 5. When we enter the loop we notice that the loop will be active as long as this variable (vol) is less than 10. We enter inside the loop, write out "Count is 5" and in the next line the count becomes 6 (incremented in the cfset). Now we go back and check the condition; it is now 6 but still less than 10, so you enter the loop again and so on until vol becomes 10. Since 10 cannot be less than 10, we are stopped. This should remind you of the while() ..wend construct in Visual Basic, or similar constructs in JavaScript.

    <cfset vol=5>
    <cfloop condition="#vol# lt 10">
    <cfoutput>Count is  #vol#</cfoutput><br/>
    <cfset #vol#=#vol#+1>
    </cfloop>
    

    The displayed output when this runs is shown here. Notice that 10 did not print.

    
    Count is 5
    Count is 6
    Count is 7
    Count is 8
    Count is 9

    More ColdFusion Articles
    More By Jayaram Krishnaswamy


       · I again welcome you to the CF tutorials. In these tutorials, I want to present a...
     

    COLDFUSION ARTICLES

    - How to Access a SQL Anywhere Database with C...
    - CFXML: Probing XMLDOM in ColdFusion
    - Creating a Web Service with ColdFusion: the ...
    - CFAjax: What it is and How to Use it
    - Querying SQL 2000 Server from ColdFusion
    - Introduction to ColdFusion Markup Language, ...
    - Introduction to ColdFusion Markup Language
    - Databases and Dreamweaver MX 2004, concluded
    - Databases and Dreamweaver MX 2004
    - Welcome to Coldfusion MX 6.1, concluded
    - Welcome to Coldfusion MX 6.1
    - What You Must Know About ColdFusion Flow-Con...
    - What You Must Know About Operators in ColdFu...
    - Everything You Must Know About ColdFusion Va...
    - My First Application on ColdFusion MX Server







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT