Loops in CF Michael Smith President TeraTech, Inc ColdFusion, Database & VB custom development 800-447-9120.

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Introduction to JavaScript for Python Programmers
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Loops in CF: To loop or not to loop? Neil Ross
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
If statements while loop for loop
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
PHP Constructs Advance Database Management Systems Lab no.3.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Loops in CF Sandra Clark Senior Software Developer Constella Group
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Learning Javascript From Mr Saem
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Error Handling Michael Smith President TeraTech, Inc ColdFusion, Database & VB custom development
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
JavaScript: Control Statements I
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Arrays, For loop While loop Do while loop
Control Structures: for & while Loops
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
REPETITION Why Repetition?
Presentation transcript:

Loops in CF Michael Smith President TeraTech, Inc ColdFusion, Database & VB custom development

Introduction n Loops - many types n Many uses

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

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

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

1.1 FOR Loops n “FOR NEXT” loop <CFLOOP INDEX="parameter_name" FROM="beginning_value" TO="ending_value" STEP="increment"> Lines to repeat Is INDEX LT TO? If so loop again INDEX = FROM INDEX = INDEX + STEP

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

FOR Loop Example 1 The loop index is #LoopCount#. (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.

FOR Loop Example 2 HTML list boxes of hours that goes from 0 to 23 #hour#

Nested Loop Example List box with all the hours and minutes of the day. #hour#:#minute# That is 1440 items! Not a good idea for a real site

1.2 WHILE Loops n “DO WHILE” loop Is condition true? If so loop again

WHILE Loop details n FOR and LIST loops are executed a certain number of times n WHILE loops are executed while a condition is true Statements to loop through

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

Conditional operators n GT, LT, GTE, LTE, EQ, NEQ, IS, CONTAINS are the relational operators supported by ColdFusion n (don’t use > etc because CFML uses >) n AND, OR, NOT are the logical operators supported by ColdFusion n Use ()s to group expressions

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

CFBREAK n CFBREAK exits the current loop More code here

1.3 Query Loops n CFOUTPUT n CFLOOP n CFMAIL

CFOUTPUT Query Loop SELECT FROM Customer #Get . # Variable available:  Queryname.currentrow  Queryname.recordcount Any records left? If so loop again Start at first record

CFLOOP Query Loop SELECT FROM Customer #Get . # Any records left? If so loop again Start at first record

CFMAIL loop Send one for each record in the query SELECT FROM Customer <CFMAIL QUERY="Get " TO="#Get . #" SUBJECT=“Test” SERVER="smtp.mycompany.com"> Hi There Any records left? If so loop again Start at first record

Nested Query Loop Example SELECT , SecurityLevel FROM Customer SELECT Text, Subject FROM Messages WHERE SecurityLevel = #Get .SecurityLevel# <CFMAIL QUERY="GetText" TO="#Get . #" SUBJECT="#GetText. Subject#" SERVER="smtp.mycompany.com">#GetText. Text#

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

1.4 List Loops n “FOR EACH” loop <CFLOOP INDEX="ListElement" LIST="#form.state#" DELIMITERS=","> #ListElement# u Other delimiters than comma are allowed Any items left in list? If so loop again Start at first item in list

How list loops work n LIST loops allow you to list the values for the control variable instead of computing them as in the FOR loop <CFLOOP INDEX=“list_variable” LIST=“value_list”> Statements to loop through

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

List Loop example n List local states <CFLOOP INDEX=“StateName” LIST=“MD, VA, DC”> #StateName# Produces…. MD VA DC

Text file as a “list” n Text file processing by line can be done using lists n Read in text file using CFFILE n Use CR LF as delimiter n The list elements are now the lines in the file.

1.5 Structure Loops n Loop over all people in the Department #person#, #StructFind(Departments, person#) Any items left in structure? If so loop again Start at first item in structure

1.6 COM Loops n FOR EACH OBJECT Loops #file2.name#

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

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

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

CFSCRIPT for n for (inital-expression; test-expression; final-expression) statement n Evaluates the initial expression. n Evaluates the test-expression. n If the test-expression is False, exits the loop and processing continues following the statement.If the test-expression is True: u Executes the statement (or statement block). u Evaluates the final-expression. u Loops statement can be a single semicolon terminated statement or a statement block in curly braces.

for loop example n Assign array a(1) =1 etc for(index=1; index LT 10; index = index + 1) a[index]=index;

Infinite for loop example n Search for key using break to stop infinite loop indx=0; for( ; ; ) { indx=indx+1; if(Find("key",strings[indx],1)) { WriteOutput("Found key at " & indx & ". "); break; } else if (indx IS ArrayLen(strings)) { WriteOutput("Exited at " & indx & ". "); break; } }

Resources n CFDOCS n Ben Forta Books n md.org/articles/introCF-4-Lists.cfm n

Questions n