天天看点

coldfusion 条件表达式、循环基本用法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

</head>

<body>

 <!--- 让coldfusion强制转换成utf-8编码格式--->

    <cfprocessingdirective pageencoding="utf-8">

 <!--- 用cfset标签定义局部变量--->

 <cfset var1="first" />

 <!--- 用cfparam标签定义局部变量--->

 <cfparam name="var2" default="second" />

 <!--- 输出到页面语句 --->

 <cfoutput>today is tuestday!</cfoutput><br />

 <cfoutput>The First Variable:#var1#</cfoutput><br />

 <cfoutput>The Second Variable:#var2#</cfoutput><br />

 <!--- 条件语句cfif、cfswitch--->

  <!--- 条件表达1 --->

  <cfif var1 eq var2>

   var1 is equal to var2!<br />

  <cfelse>

   var1 is not equal to var2!<br />

  </cfif>

  <!--- 条件表达2 --->

  <cfif "t" eq "tfq">

   t is equal to tfq!<br />

  <cfelseif "tfq" eq "tfq">

   tfq is equal to tfq!<br />

  <cfelse>

   tfq is not equal to tfq!<br />

  </cfif>

  <!--- 布尔条件表达式 --->

  <cfif 1 Is 1>

   1=1 <br /> 

  <cfelse>

   1!=1

  </cfif>

  <!--- cfswitch条件词句,在此结构中要注意expression这个关键字,千万别写错了 --->

  <cfset var3="tangfuqiang" />

  <cfswitch expression=#var3#>

   <cfcase value="tang">

    tang is equal to tangfuqiang <br />

   </cfcase>

   <cfcase value="tangfu">

    tangfu is equal to tangfuqiang!<br />

   </cfcase>

   <cfcase value="tangfuqiang">

    tangfuqiang is equal to tangfuqiang!<br />

   </cfcase>

   <cfdefaultcase>

    all values are not equal to tangfuqiang!<br />

   </cfdefaultcase>

  </cfswitch>

 <!--- 循环语句  from=1 to =14与java 中i=1;i<=14 --->

  <cfloop index="loopCout" from=0 to =14>

   <!--- 如果索引等于0,则打印0=0且跳出循环 --->

   <cfif #loopCout# Is 0>

    0=0 <br />

    <cfbreak>

   <cfelse>

    0!=0 <br />

   </cfif>

   the cfloop index is=<cfoutput>#loopCout#</cfoutput><br />

  </cfloop>

  <!--- 创建一个数组 --->

  <cfset myArray=ArrayNew(1)>

  <!--- 初始化myArray数组 --->

  <cfset ArraySet(myArray,1,5,123)>

  <!--- 给数组第四个元素设置值为tangfuqiang --->

  <cfset myArray[4]="tangfuqiang">

  <cfset foundit=false>

  <cfset i=1>

  <cfset f=0>

  <!--- 打印数组长度 --->

  <cfoutput>#Arraylen(myArray)#</cfoutput><br />

  <!--- cfdump调试打印出数组元素, <cfabort>是让后面的不执行--->

  <cfdump var="#myArray#">

  <!--- 数组长度为4的值--->

  <cfoutput>the first:#myArray[4]#</cfoutput><br />

  <cfloop condition="(Not foundit) And (i lt Arraylen(myArray))">

   <cfset f=f+0>

   <!--- 访问数组与java类似--->

   <cfif myArray[i] eq "tangfuqiang">

    <cfset f=f+1>

    <cfset foundit=true>

    <cfbreak>

   </cfif>

   <cfoutput>#i# - #myArray[i]#<br></cfoutput>

   <cfset i=i+1>

  </cfloop>

  <cfoutput>

   i is #i#<br />

   f is #f#<br />

   foundit id #foundit#<br />

  </cfoutput>

  <!--- Set the variable CountVar to 0. --->

  <cfset CountVar = 0>

  <!--- Loop until CountVar = 5. --->

  <cfloop condition = "CountVar LESS THAN OR EQUAL TO 5">

     <cfset CountVar = CountVar + 1>

      The loop index is <cfoutput>#CountVar#</cfoutput>.<br>

  </cfloop>

   <!--- 格式化日期字符串--->

  <cfoutput>#dateformat(now()<!---,"dddd,mmmm yyyy"--->)#</cfoutput>

</body>

</html>