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

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

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

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

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

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