Loops in CF - PowerPoint PPT Presentation

About This Presentation
Title:

Loops in CF

Description:

Use WHILE loops when you want to execute a set of statements as long ... Operator precedence. IS, EQ, NEQ, LT, LE, GT, GE, CONTAINS. NOT. AND. OR. 16. CFBREAK ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 39
Provided by: condo88
Category:
Tags: loops | precedence

less

Transcript and Presenter's Notes

Title: Loops in CF


1
Loops in CF
  • Sandra Clark
  • Senior Software Developer
  • Constella Group
  • sclark_at_constellagroup.com

2
Introduction
  • Loops - many types
  • Many uses

3
Loop Types
  • Here is what we will be covering
  • For Loops
  • While Loops
  • Query Loops
  • List Loops
  • More advanced loops not covered
  • Structure Loops
  • COM Loops

4
What to use loops for
  • Use FOR loops when you know exactly how many
    times a set of statements should be executed
  • Use LIST loops when you want to loop over
    something other than numbers
  • Use WHILE loops when you want to execute a set of
    statements as long as a condition is True
  • Use QUERY loops when you want to repeat for all
    records in a query.

5
Loop uses
  • Repeating HTML
  • Processing text
  • Outputing queries
  • Nested Loops
  • Breaking out of loops
  • Banding report lines

6
1.1 FOR Loops
  • FOR NEXT loop
  • ltCFLOOP INDEX"parameter_name"
  • FROM"beginning_value"
  • TO"ending_value"
  • STEP"increment"gt
  • Lines to repeat
  • lt/CFLOOPgt

INDEX FROM
Is INDEX LT TO? If so loop
INDEX INDEX STEP
7
FOR CFLOOP Parameters
  • INDEX -name of variable that controls loop
    execution
  • FROM - starting loop value
  • TO - ending loop value
  • STEP controls amount index variable is
    incremented (or decremented) in each loop
    iteration
  • Note Loop may execute zero times if backwards -
    for example FROM 2 TO 1

8
FOR Loop Example 1
Must have CFOUTPUT
  • ltCFLOOP INDEX"LoopCount" FROM"5" TO"1"
    STEP"-1"gt ltCFOUTPUTgt
  • The loop index is LoopCount.ltBRgt lt/CFOUTPUTgt
  • lt/CFLOOPgt
  • (Assume inside CFOUTPUT tags)
  • This produces.

The loop index is 5. The loop index is 4. The
loop index is 3. The loop index is 2. The loop
index is 1.
9
FOR Loop Example 2
  • HTML list boxes of hours that goes from 0 to 23
  • ltSELECT NAME"Hour"gt
  • ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
  •    ltCFOUTPUTgt
  •     ltOPTION VALUE"hour"gthour
  •   lt/CFOUTPUTgt
  • lt/CFLOOPgt
  • lt/SELECTgt

10
Nested Loop Example
This is 1440 items! Not a good idea for a real
site
  • List box with all the hours and minutes of the
    day.  
  • ltSELECT NAME"HourAndMinutes"gt
  • ltCFLOOP INDEX"hour" FROM"0" TO"23"gt
  •   ltCFLOOP INDEX"minute" FROM"0" TO"59"gt
  •     ltCFOUTPUTgt
  •     ltOPTION VALUE"'hourminute'"gthourminu
    te
  •     lt/CFOUTPUTgt
  •   lt/CFLOOPgt
  • lt/CFLOOPgt
  • lt/SELECTgt

11
1.2 WHILE Loops
Same syntax as CFIF conditions
  • DO WHILE loop
  • ltCFSET Dice 0gt
  • ltCFLOOP CONDITION"Dice LTE 5"gt
  • ltCFSET Dice RandRange(1,6)gt
  • ltCFOUTPUTgtdicelt/CFOUTPUTgt
  • ltBRgt
  • lt/CFLOOPgt

Is condition true? If so loop again
Final loop run has condition false!
12
WHILE Loop details
  • FOR and LIST loops are executed a certain number
    of times
  • WHILE loops are executed while a condition is
    true
  • ltCFLOOP CONDITIONwhile-conditiongt
  • Statements to loop through
  • lt/CFLOOPgt

13
WHILE Loop Parameters
  • WHILE Loops
  • CONDITION contains a logical expression that is
    evaluated before each loop iteration
  • As long as CONDITION is true the loop is
    executed
  • Tip - Make sure you change the values of
    variables used in CONDITION expression in the
    loop body otherwise infinite loop!

14
Conditional operators
  • GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the
    relational operators supported by ColdFusion
  • (dont use gt etc because CFML uses gt)
  • AND, OR, NOT are the logical operators supported
    by ColdFusion
  • Use ()s to group expressions

15
Operator precedence
  • ()
  • IS, EQ, NEQ, LT, LE, GT, GE, CONTAINS
  • NOT
  • AND
  • OR

16
CFBREAK
  • CFBREAK exits the current loop
  • ltCFSET StopIt 0gt
  • ltCFLOOP CONDITIONTRUEgt
  • ltCFSET StopIt RandRange(1,10)gt
  • ltCFIF StopIt LTE 5gt
  • ltCFBREAKgt
  • lt/CFIFgt
  • ltCFOUTPUTgtStopItlt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • More code here

17
1.3 Query Loops
  • CFOUTPUT
  • CFLOOP
  • CFMAIL

18
CFOUTPUT Query Loop
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFOUTPUT QUERY"GetEmail"gt
  • GetEmail.EmailltBRgt
  • lt/CFOUTPUTgt
  • Variable available
  • Queryname.currentrow
  • Queryname.recordcount

Start at first record
Any records left? If so loop again
19
CFLOOP Query Loop
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFLOOP QUERY"GetEmail"gt
  • ltCFOUTPUTgtGetEmail.EmailltBRgtlt/CFOUTPUTgt
  • lt/CFLOOPgt

Start at first record
CFOUTPUT required
Any records left? If so loop again
20
CFMAIL loop
  •  Send one email for each record in the query 
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email FROM Customer
  • lt/CFQUERYgt
  • ltCFMAIL QUERY"GetEmail"
  •       TO"GetEmail.Email"
  •       FROM"info_at_mycompany.com"
  •       SUBJECTTest
  •       SERVER"smtp.mycompany.com"gt
  • Hi There
  •    lt/CFMAILgt

Start at first record
Any records left? If so loop again
21
Nested Query Loop Example
  • ltCFQUERY NAME"GetEmail"   DATASOURCE"Library"gt
  •    SELECT Email , SecurityLevel  FROM Customer
  • lt/CFQUERYgt
  • ltCFLOOP QUERY"GetEmail"gt
  •    ltCFQUERY NAME"GetText" DATASOURCE"Library"gt
  •       SELECT EmailText, EmailSubject FROM
    Messages
  •       WHERE  SecurityLevel GetEmail.SecurityLev
    el
  •    lt/CFQUERYgt
  •     ltCFMAIL QUERY"GetText"
  •       TO"GetEmail.Email"
  •       FROM"info_at_mycompany.com"
  •       SUBJECT"GetText.EmailSubject"
  •       SERVER"smtp.mycompany.com"gtGetText.EmailTe
    xt
  •    lt/CFMAILgt
  • lt/CFLOOPgt

22
Other recordsets
  • You can loop over record sets from other tags
    than CFQUERY
  • CFDIRECTORY file list
  • CFPOP read email
  • CFSEARCH Verity text search
  • CFLDAP LDAP records
  • CFWDDX

23
1.4 List Loops
  • FOR EACH loop
  • ltCFLOOP INDEX"ListElement"
  • LIST"form.state"
  • DELIMITERS","gt
  • ltCFOUTPUTgtListElement
  • lt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • Other delimiters than comma are allowed

Start at first item in list
Any items left in list? If so loop again
24
How list loops work
  • LIST loops allow you to list the values for the
    control variable instead of computing them as in
    the FOR loop
  • ltCFLOOP INDEXlist_variable
  • LISTvalue_listgt
  • Statements to loop through
  • lt/CFLOOPgt

25
List Loop parameters
  • INDEX - variable that controls loop execution
  • LIST - a list of comma separated values
  • INDEX is assigned the values in list one at a
    time as the loop executes
  • DELIMITERS optional to give a delimiter other
    than comma

26
List Loop example
  • List local states
  • ltCFLOOP INDEXStateName
  • LISTMD, VA, DCgt
  • ltCFOUTPUTgtStateName
  • lt/CFOUTPUTgtltBRgt
  • lt/CFLOOPgt
  • Produces.

MD VA DC
27
Text file as a list
  • Text file processing by line can be done using
    lists
  • Read in text file using CFFILE
  • Use CR as delimiter
  • The list elements are now the lines in the file.

28
Read text file code
  • ltcffile action"READ" file"C\afile.txt"
    variable"text_file"gt
  • ltCFLOOP list"text_file" index"line"
    delimiters"CHR(13)"gt
  • ltcfoutputgtlineltbrgtlt/cfoutputgt
  • lt/CFLOOPgt
  • ltCFOUTPUTgtCount lines is
  • ListLen(text_file,"CHR(13)") lt/CFOUTPUTgt

29
1.5 Structure Loops
  • Loop over all people in the Department
  • ltCFLOOP COLLECTIONDepartments ITEM"person"gt
  • person,
  • StructFind(Departments, person)
  • lt/CFLOOPgt

Any items left in structure? If so loop again
Start at first item in structure
30
1.6 COM Loops
  • FOR EACH OBJECT Loops
  • ltCFLOOP COLLECTIONFFUNC ITEMfile2gt
  • ltCFOUTPUTgt file2.name ltBRgt lt/CFOUTPUTgt
  • lt/CFLOOPgt

31
CFSCRIPT Loops
  • CFScript is a JavaScript like language that
    provides the standard looping features of CFML
    plus a few more looping features
  • For
  • While
  • Do-while
  • For-in
  • CFScript also includes the continue and break
    statements that control loop processing.

32
CFSCRIPT Loops syntax
  • FOR loop
  • for (inital-expression test-expression
    final-expression) statement
  • WHILE loop
  • while (expression) statement
  • UNTIL loop evaluates condition at end of loop
  • do statement while (expression)

33
More CFSCRIPT loops
  • Structure loop
  • for (variable in structure) statement
  • The continue statement tells ColdFusion to skip
    to the beginning of the next loop iteration.
  • The break statement exits the current loop or
    case statement. Similar to ltCFBREAKgt
  • Note Still use LTE etc for conditionals in
    CFSCRIPT and not the JavaScript lt

34
CFSCRIPT for
  • for (inital-expression test-expression
    final-expression) statement
  • Evaluates the initial expression.
  • Evaluates the test-expression.
  • If the test-expression is False, exits the loop
    and processing continues following the
    statement.If the test-expression is True
  • Executes the statement (or statement block).
  • Evaluates the final-expression.
  • Loops

35
for loop example
  • Assign array a(1) 1 etc
  • for(index1 index LT 10 index index 1)
    aindexindex

36
Infinite for loop example
  • Search for key using break to stop infinite loop
  • indx0
  • for( )
  • indxindx1 if(Find("key",stringsindx,1))
    WriteOutput("Found key at " indx ".ltbrgt")
    break
  • else if (indx IS ArrayLen(strings))
    WriteOutput("Exited at " indx ".ltbrgt")
    break

37
Resources
  • CFDOCS
  • Ben Forta Books
  • http//www.cfug-md.org/articles/introCF-4-Lists.cf
    m
  • http//www.houseoffusion.com

38
Questions
  • sclark_at_constellagroup.com
Write a Comment
User Comments (0)
About PowerShow.com