Home » Tutorials » ColdFusion » Operators

Operators

Woo! Good choice, math it is. In most programming languages there are at least 5 groups of operators: arithmetic, assignment, increment/decrement, comparison, logical. ColdFusion has all of these operators, but we won’t get to crazy and we will only deal with the first 3.

Arithmetic Operators

Arithmetic operators in ColdFusion are super simple and work just as expected in almost any other language. There is no special integer division or characters that you have to deal with.

Example
<cfset sumVar = 3 + 4>
<cfset differenceVar = 3 - 4>
<cfset productVar = 3 * 4>
<cfset divisionVar = 3 / 4>
<cfset modulusVar = 4 % 3>
<cfoutput> 
    #sumVar# <br/>
    #differenceVar# <br/>
    #productVar# <br/>
    #divisionVar# <br/>
    #modulusVar# <br/>
</cfoutput>
Result 7
-1
12
0.75
1

See? I told you there is nothing special here, they are pretty much standard across most languages.

It is sometimes be useful to join strings. You might want to consider consider just outputting them both right next to each other. Sometimes, you just want to merge to strings into a new variable, but you cannot use the + sign when joining string. However, you can use the & to join strings or strings and numbers.

Example
<cfset myFirstVar = "This is one variable string">
<cfset mySecondVar = " and so is this.">
<cfset myJoinedVar = myFirstVar & mySecondVar >

<cfoutput>
    #myJoinedVar#
</cfoutput>
Result This is one variable string and so is this.

Assignment Operators

Listen up, I will save you a good amount of time by knowing how to use assignment operators. All assignment operators involve the = mixed with an operator to symbolize that you are using both at the same time. It tells ColdFusion that you want to do whatever the operator is to the following value to the current value. It will be easier to understand with an example:

Example
<cfset assignmentVar = 10>
<cfset assignmentVar += 5>
<cfoutput> 
    #assignmentVar#
</cfoutput>
Result 15

Notice how we didn’t have declare the variable on the right hand side of the statement. This is exactly what the assignment operators eliminate. Obviously, it wouldn’t too helpful if this just applied to addition. You can use all of the arithmetic operators in the exact same fashion as we just used the addition operator. Of course, you don’t have to use them. You could always write assignmentVar = assignmentVar + 5. But, come on. We are coders, and we are proud to be lazy.

Increment and Decrement Operators

Four possible outcomes occur from increments and decrements. Notice the placement of the increment or decrement operator around the variable.

Example
<cfset a = 3>
<cfset b = 3>
<cfset c = 7>
<cfset d = 7>
<cfoutput> 
    #++a#
    #b++#
    #--c#
    #d--#
</cfoutput>
Result 4
3
6
7

If you have ever messed with these guys before, you know what is going on here. If not, just understand that the position of the increment or decrement operator tells us when the action on the variable happens. In the first example, ++a, increments the variable before we print it out. However, in the second example, b++, we don’t increment the variable until after we print it to the screen. It’s a little bit complicated, but it is rather rare to use the incrementing or decrementing before the variable.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Operators". After Hours Programming. Accessed on March 17, 2024. https://www.afterhoursprogramming.com/tutorial/coldfusion/operators-cf/.

  • Stewart, Suzy. "Operators". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/coldfusion/operators-cf/. Accessed 17 March, 2024.

  • Stewart, Suzy. Operators. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/coldfusion/operators-cf/.



0 thoughts on “Operators”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.