Conditional Statements

Create branching paths through a program.

Conditional statements allow RapidScript program to execute different lines of code based on whether the specified condition evaluates to true.

If statement

if (condition)
{
    // code only executes when the condition is true
}

The condition is an expression that evaluates to a bool value. This can be: a bool literal, a bool variable, an expression using an operator (comparison, relational, or logical), or a function call with a bool return value.

The code within the {} only executes if the condition evaluates to true.

Else statement

If there is code to execute only when the condition does not evaluate to true, then an else statement can be added containing the desired code. The code in the else block will only execute if the condition evaluates to false.

if (condition)
{
    // code only executes when the condition is true
}
else
{
    // code only executes if previous conditions are false
}

Else-If statement

If there is more than one case to account for, then an else if statement can be added. The else if has its own condition, and will only execute its code block if all previous conditions evaluated to false and its own condition evaluates to true. The else block only executes if all of the preceding conditions evaluate to false. Multiple else if statements may be added.

if (condition1)
{
    // code only executes if condition1 is true
}
else if (condition2)
{
    // code only executes if condition2 is true and previous conditions are false
}
else if (condition3)
{
    // code only executes if condition3 is true and previous conditions are false
}
else
{
    // code only executes if previous conditions are false
}

Nested If statement

If statements may also be nested within each other.

if (outerCondition)
{
    if (innerCondition)
    {
        // code only executes if outerCondition and innerCondition both true.
    }
}

Last updated