if Statement

Home Scripting Statements if Statement

The if statement lets you make execution of statements dependent on conditions (in the form of boolean expressions).

if <boolean expression> then
  <statements>
end if

if <boolean expression> then
  <statements>
else
  <statements>
end if

In the first variant, without an else clause, the statements are executed if and only if the boolean expression evaluates to true. In the second variant, with an else clause, the first group of statements are executed if the boolean expression evaluates to true, and the second group of statements if the expression evaluates to false.

Note that so far MoStacks is not yet flexible about if statements and always requires the end if. This means that the following statement is not valid:

if x > 5 then answer "greater than 5"

There is no elseif construct. If needed you have to nest if statements, like in the following example:

if x < 0 then
  answer "less than 0"
else
  if x > 0 then
    answer "greater than 0"
  else
    answer "exactly 0"
  end if
end if

Note that formatting i.e. indentation is not strictly necessary; also some statements would be allowed together on single lines. The following example would be perfectly ok:

if x > 5 then answer "greater than 5" end if

But of course it is strongly recommended to format scripts in a sensible way, because you as a human will probably quickly loose track of the nesting of the statements without proper visual hints.