==
(equal), !=
(not equal), >
(greater than), >=
(greater than or equal to), <
(less than), and <=
(less than or equal to). You can also define the operators as abbreviations: EQ, NEQ, GT, GTE, LT, and LTE
.true
or false
, so they're used in conditional statements, for example, IsArray
which is true
only when the variable is an "array". Structures have an instruction named structKeyExists()
or keyExists()
which returns true
if a key is present in a structure. Strings can also be used for conditional operations by checking the .length()
member function.if
/ else if
/ else
expressions. Let's write an example by adding a method to our PersonalChef.cfc
class:The water is not boiling yet.
.It's just barely boiling
.It's boiling!.
Hot! Hot! Hot!
.if
block has:if
statement whose instructions are executed only if the statement is trueelse if
statements whose instructions are executed only if the statement is trueelse
statement whose instructions are executed if no if
nor else if
statements were trueif / else if / else
structure can have its instructions run. If the if is true, for instance, CFML will never look at the else if
. Once one block executes, that’s it.if, else, else if
expression statements. It is very common in other languages and can be used for a more fluent expressive conditional expression.condition
is evaluated. If it is true, then the true statement executed; if it is false, then the false statement executes.trueStatement
and the falseStatement
into more tenrary operations. However, don't abuse it as they will look ugly and just be very complex to debug.true
of course!isDefined(), structKeyExists()
and IF
statements to do these kind of evaluations. They work, but not very expressive or concise.right default
for a variable or an expression Or it is a short-hand way to do parameterization. It will allow us to set a value if the variable is Null
or does not exist.userName
does not exist or evaluates to null
then the default value of the myName
will be assigned the right part of the ?:
elvis operator -> Anonymous
key not exists
exception but returning an undefined
or null
value. You can then combine that with the elvis operator and create nice chainable struct navigation. For example instead of doing things like:?
) along with the dot operator (.
) is known as safe navigation operator(?.
). The safe navigation operator makes sure that if the variable used before the operator is not defined or java null
, then instead of throwing an error, the operator returns undefined
for that particular access.switch / case / default
block.if
statement marks the start of an if
block and contains one or more else if
statements and perhaps one (and only one) else
statement, the switch
statement marks the start of a switch
block and can contain multiple case
statements and perhaps one (and only one) default
statement.switch / case / default
can only evaluate the resulting value of a single variable or expression, while the if / else if / else
block lets you evaluate the true or false
result of different variables or expressions throughout the block.case
statements with curly braces. As best practice, do so for all case
and/or default
blockswhile( conditional )
expression allows you to execute a code block as many times as the conditional
expression evaluates to true. This is a great way to work with queues, stacks or just simple evaluations.==
and =
Common Mistake=
and ==
.=
is an assignment. It means "take what's on the right side and stick it into whatever is on the left side" (or its telling not asking.)==
is a question. It means "is the thing on the right equal to the thing on the left" (or its asking not telling.)