DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 3 · LESSON 01

if

Running code conditionally in ColdFusion with the if statement — comparison operators, Boolean conditions, and syntax in both CFScript and Tag Syntax (cfif).

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

Every real application needs to make decisions. Should a user see the admin panel? Is a cart total high enough for free shipping? Did a form field pass validation? Decision making is how a program chooses which code to run based on whether a condition is true or false.

ColdFusion's most basic decision-making tool is the if statement. It evaluates a condition and runs a block of code only when that condition is true.

By the end of this lesson, you'll understand Boolean conditions, comparison operators, and how to write if statements in both CFScript and Tag Syntax.

Learning Objectives

After completing this lesson, you'll be able to:

  • Understand what decision making means in programming.
  • Write an if statement in CFScript and Tag Syntax.
  • Use comparison operators to build conditions.
  • Understand why a condition must evaluate to a Boolean.

What Is Decision Making?

Decision making means running different code depending on a condition. A condition is an expression that evaluates to true or false — for example, age >= 18, or username == "admin".

ColdFusion's core decision-making tools are if, if...else, switch, and the ternary operator, each covered as its own lesson in this module.

The if Statement

An if statement runs a block of code only when its condition is true. If the condition is false, the block is skipped entirely.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

In Tag Syntax, <cfif> works without an <cfelse> or closing block for every branch — but the tag itself must always be closed with </cfif>.

Comparison Operators

Conditions are built with comparison operators. CFScript typically uses symbols; Tag Syntax typically uses the word form — but both forms work in either syntax.

SymbolWord FormMeaning
==EQEqual to
!=NEQNot equal to
>GTGreater than
<LTLess than
>=GTEGreater than or equal to
<=LTELess than or equal to

Combining Conditions

Use AND / && and OR / || to combine multiple conditions, and NOT / ! to negate one.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Conditions Must Be Boolean

A condition must evaluate to true or false. ColdFusion converts some values automatically — the string "Yes" and any non-zero number evaluate to true — but non-convertible values throw an error. See the Type Conversion lesson for the full conversion rules.

Real-World Example: Discount Eligibility

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Common Beginner Mistakes

Using = Instead of == or EQ

if (age = 18) is not a valid comparison in CFScript — use == for equality. In Tag Syntax, EQ (or ==) is correct inside <cfif>.

Forgetting Braces in CFScript

Without { } around a multi-line block, only the very next line belongs to the if statement, which leads to bugs when more lines are added later. Always use braces, even for a single line.

Comparing Values of Different Types Without Validating

Comparing a string form field directly to a number can throw an error if the string isn't numeric. Validate with isNumeric() first — see the Type Conversion lesson.

Best Practices

  • Always use == or EQ for equality checks, never =.
  • Always wrap if blocks in braces in CFScript, even for one line.
  • Keep conditions simple and readable — split complex logic into named variables.
  • Validate a value's type before comparing it, especially for user input.

Interview Questions

What must a condition evaluate to for an if statement?

A Boolean value — true or false.

What is the word-form equivalent of == in Tag Syntax?

EQ

What happens if the condition in an if statement is false?

The code block is skipped and execution continues after the if statement.

How do you combine two conditions so both must be true?

Use AND (or &&) between them.

Summary

In this lesson, you learned how ColdFusion makes decisions using the if statement. You wrote conditions with comparison operators, combined conditions with AND/OR, and saw why a condition must evaluate to a Boolean — in both CFScript and Tag Syntax.

What's Next?

The next lesson covers the if...else statement — how to run alternate code when a condition is false, and how to chain multiple conditions with else if.