Skip navigation.

<CFSCRIPT> Part 2 Using if and else statements in flow control

For simple flow control you could use <CFSCRIPT> along with the if and else keywords. Here is an example:

first_name = "Sam";
if(#first_name# eq "Sam") {
    WriteOutput("Hello #first_name#");
} else {
    WriteOutput("Hello Stranger!");

Here's the explanation:

Set the value of the variable first_name to "Sam". Following "if", the condition is enclosed in parentheses. Each line of code end with the semicolon (;). If the condition inside the parentheses is met, the code inside the curly braces is executed.

The if-else statements only provide for two choices. If you need more, you will need to use the switch-case statements. I'll show you examples of these in the next post.