ColdFusion If Statements

As a programmer, you will often need to use conditional statements.

A conditional statementdfn> is a piece of code that does one thing based on one condition, and another based on another condition. In fact, you could have as many conditions as you like.

ColdFusion If statements are an example of conditional statements. With If statements, you can tell the ColdFusion server to execute a piece of code only if a given condition is true.

Example If Statement

In this example, the text "Still haven't found what I'm looking for." only gets displayed if the condition is met (that the value of the variable firstName equals Bono.

The letters eq are used with <cfif> to test that a something is equal to something.

Display in browser:

Still haven't found what I'm looking for.

"If Else" Statement

You can use the <cfelse> tag to do something if the condition is not met.

Display in browser:

Choose your own song then...

Example "If Else If" Statement

You can use the <cfelseif> tag to perform another test (only if the preceeding condition/s is/are not met).

Display in browser:

Crazy Train!

NOT Equal To?

Just as eq stands for "equal to", neq stands for "not equal to".

Display in browser:

Crazy Train!

Less Than?

You can use lt to test that a value is less than another.

Display in browser:

Don't give up your day job!

Greater Than?

You can use gt to test that a value is greater than another.

Display in browser:

Give up your day job!

Greater Than Or Equal To/Less Than Or Equal To?

You can use gte to test that a value is greater than or equal to another.

You can use lte to test that a value is less than or equal to another.

Display in browser:

Give up your day job!