|
Expressions involve operators and operands. Operands can be of various types, which can be in the form of literals, variables, or results of a function call. There are four types of operators that can be used in expressions: arithmetic, string, comparison, and Boolean.
When evaluating expressions, ColdFusion uses standard operator precedence. The order of evaluation of operators is given in Table 2.3.
Operator | Type | Description |
---|---|---|
Unary +, Unary - | Arithmetic | Sets the sign of the number |
^ | Arithmetic | Raises a power to a number |
*, / | Arithmetic | Used for multiplication and division, respectively |
\ | Arithmetic | Used for integer division |
MOD | Arithmetic | Returns the remainder of integer division |
+,- | Arithmetic | Used for addition and subtraction, respectively |
& | String | Used for string concatenation |
EQ, NEQ, LT, LTE, GTE, CONTAINS, DOES NOT CONTAIN | Comparison | Returns a Boolean value after the respective comparison operations |
NOT | Boolean | Returns TRUE for a FALSE value and vice versa |
AND | Boolean | Returns TRUE when both the comparison expressions return TRUE; otherwise, it returns FALSE |
OR | Boolean | Returns TRUE when one of the comparison expression returns TRUE |
XOR | Boolean | Represents Boolean exclusive OR |
EQV | Boolean | Represents Boolean equivalence |
IMP | Boolean | Represents Boolean implication |
Unlike traditional programming languages, ColdFusion can evaluate an expression even if the operands and operators are of different types. The operands are converted to the datatypes as required by the operator. For example, the concatenation operator requires operands to be strings. If one of the operands isn't a string, it's converted to a string for concatenation.
Arithmetic expressions involve numeric data or variables as operands and arithmetic operators. The <CFSET> tag can be used to perform computations.
The <CFOUTPUT> tag is used to display the results of variable calculations on a Web page. Variable names must be enclosed within pound signs (#) when used with the <CFOUTPUT> tag. <CFOUTPUT> replaces the variable with its current value when the Web page is displayed in the browser. The following example illustrates this:
<CFEST num=20> <CFSET numtimes2=(num * 2)> <CFOUTPUT>num is: #num#</CFOUPUT><br> <CFOUTPUT>numtimes2 is: #numtimes2# </CFOUTPUT><br>
The preceding code generates the following output:
num is: 20 numtimes2 is: 40
String expressions involve string values or variables that are operated using string operators. The <CFSET> tag is used to assign a value to the string variable, as follows:
<CFSET City="Washington">
Here, "Washington" is a string literal.
To assign the result of a string expression, the <CFSET> tag is used as follows:
<CFSET FirstName="William"> <CFSET LastName="Trueman"> <CFSET FullName = FirstName & LastName>
The preceding code stores the string WilliamTrueman in the FullName variable.
Unlike other programming languages, ColdFusion lets you create string literals that span multiple lines, as follows:
" a b c "
Similar to numeric variables, string variables can also be displayed by using the <CFOUTPUT> tag. Variables that are surrounded by pound signs are replaced with their actual values.
If you want to include a quotation mark in a string, you need to type both quotation marks together, as follows:
<CFSET Name="William ""Buck"" Trueman">
The preceding code stores the value William "Buck" Trueman in the Name variable.
Boolean expressions are used when a program must take an action depending on a condition.
You can use Boolean constants in expressions directly, as follows:
<CFSET ValueIsCorrect = True>
In Boolean expressions, TRUE, non-zero numbers, and the string "Yes" are equivalent, and FALSE, 0, and the string "No" are equivalent.
Note | The pound or hash sign (#) has a special meaning in ColdFusion. It's used to separate expressions from plain text. When expressions surrounded by # signs are evaluated, the result is substituted for the expression. |
|