Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

CFIF vs CFCASE

Participant ,
Sep 30, 2007 Sep 30, 2007
I have always been using cfif to check for conditions.

I am aware of the cfswitch/cfcase tags but have never used them. Assistance from this board used these tags to help me solve my problem. In reading up on these tags, they are similar to cfif and cfloop.

When should each of these tags be used and does it matter which one is used ?

As a novice cf user, it appears there are so many similar tags that it really confuses me. Same with cfdirectory and cffile ?
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 30, 2007 Sep 30, 2007
Well, for simple If condition, cfif does the job, but if you have a long list of conditions to test against, cfswitch is far better in terms of performance.

Hope this helps,
Chris
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2007 Sep 30, 2007
ChrisVE wrote:
> Well, for simple If condition, cfif does the job, but if you have a long list of conditions to test against, cfswitch is far better in terms of performance.

that's not always true. for instance string expressions (at least for cf7) that
are looped over are slower than equivalent cfif:

http://www.webapper.net/index.cfm/2006/7/27/20060727042244
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 30, 2007 Sep 30, 2007
cfswitch is for assessing one expression and doing different operations depending on the value, while this can be done with cfif it is supposedly not as efficient. This is not always the case as PaulH touches on, this occurs when the expression you are assessing is a string, this is because in Java which everything in CF is built on top of you cannot use a switch statement to assess a string, so when you use cfswitch for this internally an exception is raised with is caught and the switch statement is converted to an equivalent if-elseif-else statement. This obviously has a detriment in performance, however this is only really evident under large load.

If most other programming languages switch should outperform if-else when appropriate, mainly people use it because it is an easier syntax to comprehend and modify when you are assessing an expression. Most people are either ignorant to or ignore the string issue unless they are having problems as it only really evident under load.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 01, 2007 Oct 01, 2007
In some cases, the syntax is a lot easier if you use neither. Instead use the cfscript equivalents. For example, you want the same code to run for more than one value of a variable, you can do this in cfscript

case "value1":
case "value2":
{code}

When using tags, you can't have two cfcase tags looking for the same value. I've never tried this, so I don't know if it would work.
<cfcase value="value1" or value="value2">
But I suspect it wouldn't.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 01, 2007 Oct 01, 2007
LATEST
If you want to values for the same case statement you can provide a list eg:-

<cfcase value="10,2" delimiter=",">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources