天天看點

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>