Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

conditionals

bcswartz edited this page · 10 revisions

Previous Structures --- Next Nothingness & Null


9. Conditionals

Conditional statements evaluate to true or false only. The most common conditional operators are == (equal), != (not equal), > (greater than), >= (greater than or equal to), < (less than), and <= (less than or equal to). You can also define the operators as abbreviations: EQ, NEQ, GT, GTE, LT, and LTE. You can only use the abbreviations when using tags.

Some instructions return a true or false, so they're used in conditional statements, for example, "IsArray" which is "true" only when the variable is an "array". Structures have an instruction named StructKeyExists which returns "true" if a key is present in a structure.

Also integers can be evaluated as true or false. In ColdFusion, 0 (zero) is false and any other integers are true.

<cfif 1>I am true so will show</cfif>

<cfif -2>I am true so will show</cfif>

<cfif 0>I am false so will not show</cfif>

9. 1. If, Else If, & Else

Why do we have conditional statements? Most often its to control conditional instructions, especially if / else if / else structures. Let's write an example by adding a method to our PersonalChef class:

Tag Syntax

<cffunction name = "water_boiling" returnType = "component">
 <cfargument name = "minutes" type = "numeric" required = "yes">

 <cfif (arguments.minutes LT 7)>
  <cfset this.status = "The water is not boiling yet." />

 <cfelseif (arguments.minutes EQ 7)> 
  <cfset this.status = "It's just barely boiling." />

 <cfelseif (arguments.minutes EQ 8)>
  <cfset this.status = "It's boiling!" />

 <cfelse>
  <cfset this.status = "Hot! Hot! Hot!" /> 

 </cfif>

 <cfreturn this />
</cffunction>

Script Syntax

public component function water_boiling(numeric minutes){
 if (arguments.minutes < 7) 
  this.status = "The water is not boiling yet.";

 else if (arguments.minutes == 7) 
  this.status = "It's just barely boiling.";

 else if (arguments.minutes == 8)
  this.status = "It's boiling!";

 else 
  this.status = "Hot! Hot! Hot!";

 return this;
}

Try this example using 5, 7, 8 and 9 for the values of minutes.

  • When the minutes is 5, here is how the execution goes: Is it true that 5 is less than 7? Yes, it is, so print out the line The water is not boiling yet..

  • When the minutes is 7, it goes like this: Is it true that 7 is less than 7? No. Next, is it true that 7 is equal to 7? Yes, it is, so print out the line It's just barely boiling.

  • When the minutes is 8, it goes like this: Is it true that 8 is less than 7? No. Next, is it true that 8 is equal to 7? No. Next, is it true that 8 is equal to 8? Yes, it is, so print out the line It's boiling!.

Lastly, when total is 9, it goes:" Is it "true" that 9 is less than 7?

No. Next, is it true that 9 is equal to 7? No. Next, is it true that 9 is equal to 8? No. Since none of those are true, execute the else and print the line Hot! Hot! Hot!.

An if block has:

  • One if statement whose instructions are executed only if the statement is true
  • Zero or more else if statements whose instructions are executed only if the statement is true
  • Zero or one else statement whose instructions are executed if no if nor else if statements were true

Only one section of the if / else if / else structure can have its instructions run. If the if is true, for instance, CFML will never look at the else if. Once one block executes, that’s it.

9. 2. Switch, Case, & Default

Another situation that involves conditional logic is when a single variable or expression can have a variety of values and different statements or functions need to be executed depending on what that value is. One way of handling this situation is with a switch / case / default block.

Much like how the if statement marks the start of an if block and contains one or more else if statements and perhaps one (and only one) else statement, the switch statement marks the start of a switch block and can contain multiple case statements and perhaps one (and only one) default statement. The main difference is that switch / case / default can only evaluate the resulting value of a single variable or expression, while the if / else if / else block lets you evaluate the true or false result of different variables or expressions throughout the block.

Tag Syntax

<cfswitch expression="#city#">

 <cfcase value="New York">
  <cfset region= "East Coast">
 </cfcase>

 <cfcase value="Los Angeles">
  <cfset region= "West Coast">
 </cfcase>

 <cfcase value="Phoenix">
  <cfset region= "Southwest">
 </cfcase>

 <cfcase value="Cleveland,Cincinnati">
  <cfset region= "Midwest">
 </cfcase>

 <cfdefaultcase>
  <cfset region= "Unknown">
 </cfdefaultcase>

</cfswitch>

Script Syntax

switch(city) {

 case "New York":
  region= "East Coast";
 break;

 case "Los Angeles":
  region= "West Coast";
 break;

 case "Phoenix":
  region= "Phoenix";
 break;

 case "Cleveland":
 case "Cincinnati":
  region= "Midwest";
 break;

 default:
  region="Unknown";
}

In this example, if the value of the city variable matches any of the American cities denoted in the case statements, that case block (and only that case block) will be executed and the value of the region variable will be assigned accordingly. The last of the three case statements will be executed if the value of city is either "Cleveland" or "Cincinnati." If the value of the city variable doesn't match any of the case statement values, the default statement will be executed, just like an else statement at the end of an if block.

9. 3. Looping

Another time we use conditional statements is when we want to repeat a set of instructions. Try out this simple example by adding it to your PersonalChef.cfc:

Tag Syntax

<cffunction name = "countdown" returnType = "component">
 <cfargument name = "counter" type = "numeric">
 <cfset this.timer = "" />
 <cfloop condition = "#arguments.counter# GT 0">
  <cfset this.timer &= "The counter is #arguments.counter#.<br />" />
  <cfset arguments.counter-- />
 </cfloop>
 <cfreturn this />
</cffunction>

Script Syntax

public component function countdown (numeric counter){
 this.timer = "";
 while (arguments.counter GT 0) { 
  this.timer &= "The counter is #arguments.counter#.<br />";
  arguments.counter--;
 }
 return this;
}

See how that works? The counter starts out as whatever parameter we pass in. The while instruction evaluates the conditional statement arguments.counter GT 0 and finds that yes, the counter is greater than zero. Since the condition is true, execute the instructions inside the loop. First print out The counter is #arguments.counter# then take the value of arguments.counter and subtract one from it. Next, we overwrite the previous value of arguments.counter with the new value. Then the loop goes back to the condition / while statement. Is it still true? If so, print the line and subtract one again. Keep repeating until the condition is false.

You can also combine conditional statements using logical operators. The most common are known as "logical and" and "logical or". In CFML you can write a "logical and" with either the word "and" or with double ampersands like this: &&. You can write a "logical or" with the word or or with double pipes like this: ||. For each operation, the symbolic representation ( && and || ) is more common.

The #1 mistake people encounter when writing conditional statements is the difference between = and ==.

  • = is an assignment. It means "take what's on the right side and stick it into whatever is on the left side" (or its telling not asking.)
  • == is a question. It means "is the thing on the right equal to the thing on the left" (or its asking not telling.)

Previous Structures --- Next Nothingness & Null

Something went wrong with that request. Please try again.