340 likes | 486 Vues
Explore the fundamentals of flow control in ColdFusion, focusing on selection constructs such as CFIF and CFSWITCH. Learn how to implement conditions using relational (GT, LT, EQ, etc.) and logical operators (AND, OR, NOT) to control the flow of your code. Gain insights into nested CFIF tags and their execution logic. This guide also covers repetition with loops (FOR, WHILE, LIST) for efficient coding when executing sets of statements multiple times, enhancing your ColdFusion programming skills.
E N D
ColdFusion Basics (Cont’d)ColdFusion Flow Control Constructs 321483
Using Selection to Control Flow • Selection is a flow-control concept where one or more alternatives are examined and a course of action is taken based on those alternatives • Selection construct is implemented in Coldfusion using CFIF and CFSWITCH tags • Conditions, relational, and logical operators are used with selection statements
Using Selection to Control Flow • Condition: • A condition is a comparison of two quantities • A condition can be either true or false • Relational Operators: • Relational operators are used to compare quantities in a condition • GT, LT, GE, LE, EQ, NEQ are the relational operators supported by ColdFusion • Logical Operators: • AND, OR, NOT are the logical operators supported by ColdFusion
CFIF Tag • CFIF tag used to set up a selection construct in ColdFusion • <CFIF condition>True Action<CFELSE>False Action</CFIF> • Click to view CFIF tag syntax
CFIF Tag • These tags must be used in sequence • If condition is true, True Action is executed and False Action is skipped • If condition is false, False Action is executed and True Action is skipped
Relational Operators and Data Types • See files cfif_relop1.cfm and cfif_relop2.cfm for some examples of how to use CFIF tag and relational operators
Nested CFIF Tags • You can enclose CFIF tags within other CFIF tags - nested CFIF tags • Example: <CFIF Condition1> <CFIF Condition2> True action 2<CFELSE> False action 2</CFIF> <CFELSE>False action 1 </CFIF>
Nested CFIF Tags • The condition in the outer CFIF tag is evaluated first - then the condition with the inner CFIF tag is evaluated as necessary • See file cfif_nested.cfm for an example of nested CFIF tags
Combining CFELSE Tag • You can combine a CFELSE and CFIF tag by using the CFELSIF tag • Example: <CFIF Condition1> Action 1<CFELSIF Condition2> Action 2<CFELSIF Condition2> Action 3<CFELSE> Action 4 </CFIF>
Combining CFELSE Tag • See file cfelseif.cfm for an example of CFELSEIF tags
Using Logical Operators • CFIF Statements can contain more than one expression to evaluate • In this case, you must separate expressions by logical operators such as AND, OR and NOT • Logical operators let you combine simple conditions to form complex ones • See file logical_op.cfm for an example of nested logical operators use
Using Logical Operators (Truth) Table of And Operator Condition1 Condition2 Condition1 AND Condition 2 T T T T F F F T F F F F
Using Logical Operators (Truth) Table of OR Operator Condition1 Condition2 Condition1 OR Condition 2 T T T T F T F T T F F F
Not(Cond1AndCond2) Not(Cond1)OrNot(Cond2) = Not(Cond1OrCond2) Not(Cond1)AndNot(Cond2) = Not(=) <> = Not(<>) = = Not(<) >= = Not(<=) > = Not(>) <= = Not(>=) < = Using Logical Operators • Not Operator
Using Logical Operators • Not Operator - Examples • Not X < 10 • Not (X >= 10 and X < 20) • Not (X =10 or Y <> 30)
Using CFSWITCH Statement • Allows you to select an action from several different actions <CFSWITCH EXPRESSION=“expression”><CFCASE VALUE=“value-list1”> Action1 </CFCASE><CFCASE VALUE=“value-list2”> Action2 </CFCASE><CFDEFAULTCASE> Action3</CFDEFAULTCASE> </CFSWITCH>
Using CFSWITCH Statement • CFSWITCH EXPRESSION attribute can be assigned any valid expression - also called selector expression • Result is the basis for the selection of the action to be performed - Value also called selector value • CFCASE VALUE attribute used to match value of expression - if a match that case is performed • CFDEFAULTCASE tag defines default action to be performed • Click to view CFSWITCH tag syntax
Using CFSWITCH Statement • See file cfswitch.cfm for an example of CFSWITCH tag use
Using Repetition • When you need to execute a set or program statements many times - use the repetition flow control statements • ColdFusion provides the CFLOOP tag to perform repetition • Used to implement FOR, LIST, WHILE, and QUERY loops
Using Repetition • Use FOR loops when you know exactly how many times a set of statements should be executed • Use WHILE loops when you want to execute a set of statements as long as a condition is True • Use LIST loops when you want to execute a set of statements repeatedly • QUERY loops are discussed later
FOR Loops • Used to execute a set of statements a predefined number of times <CFLOOP INDEX=“for_variable”FROM=“start_value”TO=“end_value”STEP=“increment_value”>Statements to loop through</CFLOOP> • Click to view For Loop tag syntax
FOR Loops • INDEX assigned name of variable that controls loop execution • FROM assigned a value or expression that provides starting loop value • TO assigned value or expression that provides ending loop value • STEP assigned to value that controls amount index variable is incremented in each loop iteration
LIST Loops • 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</CFLOOP> • Click to view List Loop tag syntax
LIST Loops • INDEX assigned name of variable that controls loop execution • LIST assigned a list of comma separated values - INDEX is assigned values in list one at a time as the loop executes • See file list_loop.cfm for an example of List Loops use
WHILE Loops • FOR and LIST loops are executed a certain number of times • WHILE loops are executed while a condition is true <CFLOOP CONDITION=“while-condition”>Statements to loop through</CFLOOP> • Click to view While Loop tag syntax
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 • See file while_loop.cfm for an example of While Loops use