This is the fourth installment of a 6 part series. All sections will help to introduce you to some basic programming concepts that will all carry through and help you as you learn ColdFusion. If you are looking for any of the other installments to this series, you can get to them below.
- What is Programming?
- Proper Syntax
- Variables and Data Types
- Conditional Processing
- Arrays and Structures
- Modular Code
You have probably dealt with conditional processing on a pretty regular basis but in real life we simply call it making a decision. As an example, if you are going to be home from work on time, then you might decide to make dinner yourself, if you are running late, then you might decide to save time by ordering in.
In the world of programming, conditional processing simply means that as the code is read by the application, it will react to any conditions it comes across and behave the appropriate way. This is the method used to control the flow of any application.
ColdFusion makes conditional processing extremely simple by providing us with the <cfif>/<cfelse>/<cfelseif> tags.
CFIF, CFELSEIF and CFELSEIF
The conditional statement when using any of the tags will always be true or false, so it is as simple as using the tags to surround an event you want to happen if it’s true.
Let’s look at some source code to better explain it. The sample code below shows some conditional processing of code based on our red hat choice previously mentioned. Remember I said I could save your color entry into a variable called “thishat”? Well, here we have some conditional processing that is checking if the hat is red and if it is then show the red hat products.
<cfif thishat is "Red"> <!--- Show the red hat products---> </cfif>
If the condition isn’t true, for example if you entered “blue”, it will simply skip over that section of code completely.
What if I wanted to check for other colors and show products based on those choices? There may also be instances where someone entered a color that I hadn’t accounted for. We can add in additional checks for those options as well.
<cfif thishat is "Red"> <!--- Show the red hat products ---> <cfelseif thishat is "Blue"> <!--- Show the blue hat products ---> <cfelseif thishat is "Green"> <!--- Show the green hat products ---> <cfelse> <!--- Show all products ---> </cfif>
As you can see in the above code, using the <cfelseif> tag lets you check for additional conditions within the same block of code and <cfelse> will be the final remaining option if none of the others are true. Just keep in mind that code is read from left to right and top to bottom. This means that as soon as the condition is met and becomes true, the conditional processing stops, runs that block of code and then picks back up after the ending </cfif> tag.
One final note about those comments I used placeholders within the <cfif> blocks. In standard HTML code, you may have seen double dashes like <!– comment –>. This hides whatever is between those dashes but still makes it available behind the scenes if you were to look at the source code. Since ColdFusion is a server side language, we can use triple dashes like this <!— comment —> and the comment will forever stay on the server side and never be shown on the client side even when viewing the HTML source. This is a great way to comment your code.
When performing conditional processing, ColdFusion gives us lots of options and operators to use to check for things. For example, I can check if variables equal a certain value, I can determine if a variable exists, I can check if a number is greater than another number and many other operations.
You will notice ColdFusion operators for conditional processing are fairly obvious to understand and are mainly abbreviations for an English word. This is because since ColdFusion is a tag based language and uses the “<” and “>” which makes it impossible to use those same symbols to designate greater than or less than. Instead of using symbols we are going to be using letters.
- GT – Checks if the value on the left is greater than the value on the right
- GTE – Same as above but checks if it is greater than OR equal to
- LT – Checks if the value on the left is less than the value on the right
- LTE – Same as above but checks if it is less than OR equal to
- IS/EQ – Both check if a value is equal to the other
In the next section we will be covering arrays and structures, so when you’re ready continue onto the next section.