ColdFusion Programming/control structures

< ColdFusion Programming

As in most programming languages Coldfusion includes control structures to help your program be more useful.

CFIF

The most basic control structure is the CFIF. It works exactly like an If statement in other programming languages.

A sample CFIF statement would look like this:

<cfif 2 gt 1>
  First
<cfelse>
  Second
</cfif>

The result would be: First

CFSWITCH

Like with other programming languages Coldfusion provides a switch functionality.

Sample CFSWITCH:

<cfswitch expression="bob">
  <cfcase value="george">
    George
  </cfcase>
  <cfcase value="bob">
    Bob
  </cfcase>
</cfswitch>

The result would be: Bob

CFLOOP

Index Loop:

Example:

  <cfloop from="1" to="9" index="i">
    #i#
  </cfloop>

Results: 1 2 3 4 5 6 7 8 9

  <cfloop from="1" to="9" index="i" step="2">
    #i#
  </cfloop>

Results: 1 3 5 7 9


Conditional Loop:

Example:

  <cfset i = 2>
  <cfloop condition="i lt 10">
    #i#
    <cfset i = i + 2>
  </cfloop>

Results: 1 3 5 7 9


Query Loop:

Query getpeople looks like this:
Age Name
10 Bill
25 Martha
30 Judy

Example:

  <cfloop query="getpeople">
    #Age#, #Name#
  </cfloop>

Results:
10, Bill
25, Martha
30, Judy


List Loop:

List people looks like this: (Bill,Martha,Judy)

Example:

  <cfloop list="people" index="i">
    #i#!
  </cfloop>

Results: Bill! Martha! Judy!


Collection Loop:

Structure place looks like this:
Zip Name
55057 Northfield

Example:

  <cfloop collection="#place#" item="i">
    <cfoutput>#i#-#place[i]#</cfoutput>,
  </cfloop>

Results:
Zip-55057, Name-Northfield

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.