Home arrow ColdFusion arrow What You Must Know About Operators in ColdFusion!

What You Must Know About Operators in ColdFusion!

The fabric of a program consists of expressions, large and small, stitched together with operators and interspersed with functions. The larger expressions as well as the functions themselves may be the result of development from a number of smaller expressions. An understanding of the operators and their correct usage is therefore very essential. This tutorial describes the various operators by presenting comprehensive examples of their usage tested on the MX 6.1 server. <CFScript> will be used in all the examples.

Author Info:
By: Jayaram Krishnaswamy
Rating: 4 stars4 stars4 stars4 stars4 stars / 21
August 22, 2005
  1. · What You Must Know About Operators in ColdFusion!
  2. · Conditional Operators
  3. · Logical Operators
  4. · String Operator

print this article

What You Must Know About Operators in ColdFusion!
(Page 1 of 4 )

ColdFusion like other programming languages has many operators of the following basic types:

  • Arithmetic Operators
  • Conditional Operators
  • Logical Operators
  • String Operator
Arithmetic Operators

When dealing with numbers it is essential to understand how the operators function. Different programming languages use the symbols in slightly different ways. For example the unary operators in ColdFusion do not work the same way as the unary operators in JavaScript. ColdFusion arithmetic operators are mostly binary. Generally, two operands, operand 1 and operand 2 are operated upon by placing the operator in between them, [Operand 1][operator][operand 2] and the resulting value depends on the type of operator.

Operators Description Examples & Comments
+, -, *, / Basic add, subtract, multiply and divide (the right hand operand cannot be zero)
  • Add: #x# + #y#
  • Subtract: #x# - #y#
  • Multiply: #x# * #y#(x*y = y*x)
  • Divide: #x# / #y#
+, - Used just to change the sign of variable. i.e, to change 5 to -5
  • -#x# ( to get a negative of x).
  • <Operator><Operand>
MOD Remainder left after a division
  • Example: 5 mod 2 is 1
  • Sign of modulus is sign of dividend
  • The right operand cannot be zero
^ : the Caret Exponentiation (raising the power)
  • Example: #x# ^ 2(exponent is 2)
  • The exponent can be floating point, negative
 Represents integer division
  • Example: #x# #z#
  • For floating point numbers the integer part is used.
Examples using arithmetic operators

The following example shows several of the operators and their usage.

<!--- Arithmetic Operators --->
<!---Arith.cfm                   --->
<!--- Author: Jay Krishnaswamy --->
WriteOutput("<b>Arithmetic Operations</b><br/>");
writeoutput("There are " & #apples# & " Apples <br/>");
writeoutput("There are " & #oranges# & " Oranges<br/>");
writeoutput("There are " & #bananas# & " bananas<br/><hr width='40%'' 
align=left>"); //Inside <cfscript/> use double slash for comments //Addition WriteOutput("Total of apples and bananas = " & #apples#+#bananas# ); WriteOutput("<br/>"); WriteOutput("How many more oranges than apples?: " & # oranges#-#apples#); WriteOutput("<br/>"); WriteOutput("3 times the number of apples are " & 3*#apples# & " apples"); WriteOutput("<br/>"); WriteOutput("Half the number of oranges are " & #oranges# /2 & " oranges"); WriteOutput("<br/>"); WriteOutput("5 divided by 0 is:" ); WriteOutput( 5/0 & "<br/>"); </cfscript>

<cfset .../> tags and <cfoutput> ..... </cfoutput> tags can also be used, but you may have to write a lot of such tags for the example shown.

Browsing arith.cfm

The following picture shows the display when browsed with FireFox. It appears that FireFox can display the resulting html up to the line which has the error, but IE 6.0 just gives an HTTP 500 error. For displaying in IE 6.0, the last line needs to be modified.

blog comments powered by Disqus

- Adobe ColdFusion Just Got More RAD
- How to Access a SQL Anywhere Database with C...
- CFXML: Probing XMLDOM in ColdFusion
- Creating a Web Service with ColdFusion: the ...
- CFAjax: What it is and How to Use it
- Querying SQL 2000 Server from ColdFusion
- Introduction to ColdFusion Markup Language, ...
- Introduction to ColdFusion Markup Language
- Databases and Dreamweaver MX 2004, concluded
- Databases and Dreamweaver MX 2004
- Welcome to Coldfusion MX 6.1, concluded
- Welcome to Coldfusion MX 6.1
- What You Must Know About ColdFusion Flow-Con...
- What You Must Know About Operators in ColdFu...
- Everything You Must Know About ColdFusion Va...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials