Skip to content

Conditionals

Conditionals

Conditionals can be used to decide what the bot should do based on input it receives or the state that it is in at a given moment. Bubblescript supports several methods of describing these conditions. The preferred method is a branch. Conditionals are written down with the aid of logical expressions in the form of predicates. The following operators can be used to write logical expressions:

The following operators can be used:

  a && b    # AND
  a || b    # OR
  a !  b    # NOT
  a == b    # IS
  a != b    # IS NOT
  a <= b    # IS SMALLER OR EQUAL TO
  a >= b    # IS LARGER OR EQUAL TO
  a <  b    # IS SMALLER
  a >  b    # IS LARGER

branch

The branch statement is used for branching the control flow into different directions. It has two variants, the "bare" branch and a "variable" branch.

The "bare" branch statement evaluates each expression top down until the first one that evaluates to true. The expressions are separated by -> and then 1 or multiple lines of indented code that is executed sequentially.

This is especially handy if you want to check more than two predicates:

In the above example the expecting: assures that answer can only contain the 3 cases we branch on. If you branch on an open answer, you can use else to catch any other answers that are given:

  ask "What is your name?"
  branch do
    answer == "Steve" -> say "Hi Steve!"
    answer == "Pete"  -> say "Hello Peter"
  else
    say "hi stranger"
  end

It occurs much that you are branching on the same variable. In that case, you can use a shorthand syntax, the "variable" branch statement:

  ask "What is your name?"
  branch answer do
    "Steve" -> say "Hi Steve!"
    "Pete"  -> say "Hello Peter"
  else
    say "hi stranger"
  end

This example is exactly identical as the one before, but here we specify that we want to compare answer variable in each of the branches. The comparison is done with the Bubblescript == operator.

if

To allow the bot to make decision based on the users input the statement if and else used to conditionally execute blocks of code with if you can test for truthiness of a condition (predicate).

  if (platform == "Yes" || app == "Yes") do
    say "Apps or platforms are not really a thing anymore."
  end

Reads: "If variable platform is equal to "Yes" and variable app is equal to "Yes" do the following"

else

Executes the else block if the if statement evaluates to false (is not true).

  if exits != "Yes" do
    say "I believe in your product. Let's do it!"
  else
    if team > 3 do
      say "I think your team is too big."
    else
      if platform == "Yes" || app == "Yes" do
        say "Apps or platforms are not really a thing anymore."
      end
    end
  end