Skip to main content
Skip table of contents

Conditional Execution (if Statement)

Conditional execution means that a part of your script may or may not be executed, depending on a condition which you define. It is connected to the keywords “if” and “else”. Hence, it is often referred to as an “if instruction.”

Overall Structure

CODE
// short: all in one line
if (another_condition) {if_part}

// normal: if ... else ...
if (condition)
{
  if_part
}
else
{
  else_part
}

// multiple (several conditions): if ... elseif ... else
if (condition_1)
{
  if_part_1
}
elseif (condition_2)
{
  if_part_2
}
else
{
  else_part
}

Line breaks and and indentation as in this example are optional and make the code easier to read.

The “else” keyword, along with the following curly braces and the “else_part,” can be omitted.

The round parentheses around the condition, and curly braces around “if_part” are mandatory, even if “if_part” is empty.

If the “else” keyword is present, then the curly braces around the “else_part,” even if empty, are mandatory.

The “if_part” and (if present) “else_part” are a sequence of instructions. This sequence may contain nested “if” statements. There is no practical limit to the nesting depth.

As shown in the third example, several conditions can be chained, using the “elseif” keyword, which can also be written as “else if.”

Condition

The condition controls whether the “if_part” or (if present) the “else_part” is executed. In most cases, the condition compares a value against another value. However, you can precalculate the condition and store it in a variable, and then just write the variable’s name into the parentheses.

The following example only computes x/y if y is not 0:

CODE
if (%y!=0) {$res = %x/%y}

The sequence != reads as "is not equal to" and is called the "inequality operator." Read more about this and other comparison operators at DBM Scripting Basics.

The “condition” must be a value or expression that evaluates to a non-zero number or a non-empty string to count as “fulfilled” (“true”). If the condition is “true,” the “if_part” instruction sequence is executed, and the “else_part” is skipped. Otherwise, i.e. if the condition evaluates to zero or to an empty string, it is considered “not fulfilled” (“false”), and the “if_part” is skipped and the “else_part” (if present) is executed.

If and Else Parts

“if_part” and “else_part” are normal instruction sequences. They can contain anything that is allowed in the script, including more “if” statements, function and procedure calls, or “while” statements.

Note that “if_part” and “else_part” define separate scopes for local variables. See DBM Scripting Basics for details on variable scopes.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.