1 / 34

ColdFusion Basics (Cont’d) ColdFusion Flow Control Constructs

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

miette
Download Presentation

ColdFusion Basics (Cont’d) ColdFusion Flow Control Constructs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ColdFusion Basics (Cont’d)ColdFusion Flow Control Constructs 321483

  2. 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

  3. 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

  4. 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

  5. CFIF Tag

  6. 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

  7. Relational Operators

  8. Relational Operators and Data Types

  9. 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

  10. 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>

  11. 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

  12. 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>

  13. Combining CFELSE Tag • See file cfelseif.cfm for an example of CFELSEIF tags

  14. 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

  15. 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

  16. 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

  17. Not(Cond1AndCond2) Not(Cond1)OrNot(Cond2) = Not(Cond1OrCond2) Not(Cond1)AndNot(Cond2) = Not(=) <> = Not(<>) = = Not(<) >= = Not(<=) > = Not(>) <= = Not(>=) < = Using Logical Operators • Not Operator

  18. Using Logical Operators • Not Operator - Examples • Not X < 10 • Not (X >= 10 and X < 20) • Not (X =10 or Y <> 30)

  19. Logical Operator Precedence

  20. 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>

  21. Using CFSWITCH Statement

  22. 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

  23. Using CFSWITCH Statement • See file cfswitch.cfm for an example of CFSWITCH tag use

  24. 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

  25. 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

  26. 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

  27. FOR Loops

  28. 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

  29. 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

  30. LIST Loops

  31. 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

  32. 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

  33. WHILE Loops

  34. 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

More Related