ColdFusion
  Home arrow ColdFusion arrow Page 3 - What You Must Know About ColdFusion Flow-C...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
COLDFUSION

What You Must Know About ColdFusion Flow-Controls
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2005-09-14

    Table of Contents:
  • What You Must Know About ColdFusion Flow-Controls
  • What if, and where to scenarios
  • Which way to go?
  • Keep looking until you find
  • Breaking out of loops
  • Call a halt and end

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    What You Must Know About ColdFusion Flow-Controls - Which way to go?


    (Page 3 of 6 )



    The conditional tags <cfswitch/>, <cfcase>, and <cfdefaultcase>

    In the previous case of the <cfelseif> tag it was possible to test for two conditions which provided one of two paths to take, depending on the outcome of the test. This is clearly not the correct approach if you need to test for several conditions.

    The <cfswitch/> tag allows you to have this possibility in a much more elegant manner. Depending on the outcome of the test, you have the opportunity to proceed in a number of ways, each way decided by a <cfcase> tag. What if none of the conditions you tested gave a positive outcome? Well, no need to be disheartened; there is a <cfdefaultcase> case that will take care of how you should proceed if none of the conditions were satisfied. The <cfdefaultcase> is optional, and its position in the <cfswitch/> block need not be at the end before the </cfswitch> tag. The <cfcase> and <cfdefaultcase> tags are always used within the <cfswitch/> tag

    Example 7
    <cfset test="C++">
    <cfswitch  expression=#test#>
    <cfcase value="vb">
    You are a Visual Basic Guy
    </cfcase>
    <cfcase value="C++">
    You are a C++ Programmer
    </cfcase>
    <cfcase value="sql">
    You are a SQL Programmer
    </cfcase>
    <cfdefaultcase>
    I don't think you are a VB, C, or SQL Programmer
    </cfdefaultcase>
    </cfswitch>
    

    The displayed output by browsing the above file is as shown next. The declared value of test found a match for the case whose value was C++.

    You are a C++ Programmer

    Example 8

    The variable name is switched around until it matches with the constant value of one of the case statements, and when the match is found the line/lines under that case is/are processed. If break is not in place, the immediately following case will be tested. The default is usually the last statement before the switch ends, but could be anywhere. If it's not in the end, it must be followed by a break.

    Please refer to the Macromedia site for any unresolved issues and other restrictions with the usage of Switch. Although end switch has been added as required by the documentation, it appears to be optional, i.e., the code runs even without this.

    <cfscript>
    name="John";
    switch(name){
    case "Tom":
    writeoutput ("Name is Tom");
    break;
    case "June":
    writeoutput ("Name is June");
    break;
    case "John":
    writeoutput ("Name is John");
    break;
    default:
    writeoutput ("Name unknown");
    }//end switch
    </cfscript>
    

    The displayed output when you run this code is:

    Name is John

    More ColdFusion Articles
    More By Jayaram Krishnaswamy


       · I again welcome you to the CF tutorials. In these tutorials, I want to present a...
     

    COLDFUSION ARTICLES

    - How to Access a SQL Anywhere Database with C...
    - CFXML: Probing XMLDOM in ColdFusion
    - Creating a Web Service with ColdFusion: the ...
    - CFAjax: What it is and How to Use it
    - Querying SQL 2000 Server from ColdFusion
    - Introduction to ColdFusion Markup Language, ...
    - Introduction to ColdFusion Markup Language
    - Databases and Dreamweaver MX 2004, concluded
    - Databases and Dreamweaver MX 2004
    - Welcome to Coldfusion MX 6.1, concluded
    - Welcome to Coldfusion MX 6.1
    - What You Must Know About ColdFusion Flow-Con...
    - What You Must Know About Operators in ColdFu...
    - Everything You Must Know About ColdFusion Va...
    - My First Application on ColdFusion MX Server






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT