Statements in CFScript


These are the statements included in CFScript:

  • Assignment statements

  • Conditional statements

  • Looping statements

Assignment Statements

Assignment statements create and assign values to variables, similar to the CFML <cfset> tag. The syntax of the assignment statement is

 [variable_name] = [value] or [expression]; 

  • [variable_name] corresponds to the name of the variable that you want to create.

  • [value] corresponds to a value, which can be numeric, a string, or an object.

  • <expression> corresponds to an expression containing variables and operators, such as +, -, or EQ, that returns a value.

You can use the following code to assign different datatypes to a CFScript variable:

 <HTML> <HEAD> </HEAD> <BODY>     <cfscript>       Var1 = 20;  // This is an integer variable       Var2 = 34.4; // This is a floating point variable.       Var3 =" This is plain string" // A string value       WriteOutput("A integer variable: " & var1  & "<br>");       WriteOutput("A Real variable: " & var2 & "<br>");       WriteOutput("A String variable: " & var3);     </CFSCRIPT> </BODY> </HTML> 

You can also assign the values returned from expressions to variables. The following code illustrates how to assign expression values to variables:

 <HTML> <HEAD> </HEAD> <BODY>     <cfscript>       Var1 = 20;  // This is an integer variable       Var2 = 34.4; // This is a floating point variable.       addvar = var2 + var1; // A simple expression adding the values of two variables       WriteOutput("A variable created from a              expression adding two variables:" & addvar & "<br>");     </CFSCRIPT> </BODY> </HTML> 

Conditional Statements

Conditional statements execute a block of code depending upon the result of a conditional expression. CFScript provides these two conditional statements:

  • if and else statements

  • switch, case, and default statements

Using if and else Statements

These statements execute a code block when a specified condition is satisfied. The syntax of if and else statements is

 If ([expression]) {     [statements] } else {     [statements] } 

In the preceding code syntax:

  • [expression] is used by an if statement to determine whether a code block should run.

  • [statements] are executed when a particular conditional expression is satisfied.

The following code shows how to use the if and else statements to check whether the value of an integer variable is equal to 10:

 <cfscript>     thisvar= 10;     if ( thisvar EQ 10)     {       WriteOutput("The value of var1 is equal to 10" );     }     else     {       WriteOutput("The value of var1 is not equal to 10" );     } </CFSCRIPT> 

Using the switch Statement

This statement checks whether a variable or expression is equal to a value specified in the case statement. If any of the case statements return a TRUE value, the code statements corresponding to them are run. The syntax of the switch statement is

 switch ([expression] or [variable]) { case [constant_value]: [statements];break; case [constant_value2]: [statements];break; default:[statements]; } 

In this code syntax:

  • [expression] or [variable] is the expression or variable that is checked by the case statement for equality.

  • case [constant_value] checks whether a constant value is equal to the expression or variable specified in the switch statement.

  • [statements] are the code statements that run after a particular case statement returns a TRUE value.

  • break is used at the end of the case statement to exit the switch statement after the code statements have been processed.

  • default runs code statements if none of the case statements return a TRUE value.

The following code illustrates the use of the switch and case statements:

 <cfscript>     thisvar= 10;     WriteOutput("The variable is <b>");     switch (thisvar + 1)     {       case 10: WriteOutput ("equal to 10");break;       case 11: WriteOutput ("equal to 11");break;       case 12: WriteOutput ("equal to 12");break;       case 13: WriteOutput ("equal to 13");break;       default: WriteOutput ("Not equal to anything");     }     WriteOutput("</b>"); </CFSCRIPT> 

Looping Statements

CFScript allows you to loop through code statements by using these common looping constructs:

  • for statement

  • while statement

  • do-while statement

for Statement

This statement executes particular code statements a specified number of times. The syntax of the for statement is

 for  (initial_expression; conditional_expression; incrementing expression) {     [statements] } 

In the preceding code syntax:

  • initial_expression is the expression evaluated at the start of the for loop.

  • conditional_expression is the expression that is evaluated. If this expression returns a TRUE value, the statements inside the for loop continue to execute. The loop exits when this expression returns a FALSE value.

  • incrementing_expression runs a statement every time that the loop runs.

The following code uses the for statement to show a text output to the browser 10 times:

 <HTML> <HEAD></HEAD> <BODY>     <cfscript>       thisvar= 10;       for (i =0 ;i LT 10;i = i + 1)       {         WriteOutput("Value " & i&" <br>");       }       WriteOutput("The above loop has run for 10 times");     </CFSCRIPT> </BODY> </HTML> 

while Statement

This statement executes a block of statements until a specified expression returns a TRUE value. These statements exit the loop when the expression returns a FALSE value.

Note

The expression specified in the while loop should first return a TRUE value at the start of the loop so that the statements inside the while loop are processed. If the initial value of the expression specified is FALSE, the statements inside the while loop will not be processed.

The syntax of the while loop statement is

 While ([expression]) {     [statements] } 

In the preceding code syntax, [expression] is the expression that is evaluated in the while loop. The while loop runs until the expression returns a TRUE value.

Note

The absence of a FALSE value in the expression specified in the while loop leads to an infinite loop. This may cause your Web browser to hang.

The following code illustrates how the while loop statement runs a loop until the value of a variable is TRUE:

 <HTML> <HEAD></HEAD> <BODY> <cfscript>     thisvar= true;     i =0 ;     while (thisvar is TRUE)     {       i = i +1;       if (i EQ 10) thisvar = false;       WriteOutput (" The while loop has been running for " & i & " times<br>");     } </CFSCRIPT> </BODY> </HTML> 

do-while Loop

This loop executes the statements inside the loop before evaluating a specified expression. It continues looping through statements until the expression returns a FALSE value. The syntax of the do-while loop statement is

 do {     [statements] } while ([expression]); 

The following code uses the do-while loop to execute a block of statements until the value of a variable is less than 10:

 <HTML> <HEAD></HEAD> <BODY> <cfscript>     thisvar= true;     i =0 ;     do     {       i = i +1;       if (i EQ 10) thisvar = false;       WriteOutput (" The do while loop has been running              for "& i & " times<br>");     }     while (thisvar is TRUE); </CFSCRIPT> </BODY> </HTML> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net