Statements & Loops

Edit

Introduction #

Conditional statements are used to perform different actions based on different conditions.
If a condition is true, you can perform one action and if the condition is false, you can perform another action.

  • If - Commands to be executed if the specified condition is true.
  • Else - Commands to be executed if the specified condition is false.
  • And - All specified conditions need to be true.
  • Or - At least one of the specified conditions needs to be true.
  • Switch - Perform different actions based on different conditions (cases).

Difference between If/Else and Switch #

If/Else statement takes a specific condition and checks whether the condition is truthy or falsy. If the condition is true, then the if statement executes a specific code block. If the condition is false, then the else statement executes a different code block.

  • Example of checking what color a viewer chose with If/Else:
    If color == "green" { execute block of commands }
       Else if color == "yellow" { execute block of commands }
          Else if color == "blue" { execute block of commands }
             Else if color == "pink" { execute block of commands }
    
    Nested If/Else Statements
    Nested If/Else Statements

As you can see, you would need to nest multiple If/Else statements to check for all the possible colors. This could get quite unreadable.

Switch statement is a multiple-choice selection statement. You decide what to execute for each case.

  • Example of checking what color a viewer chose with Switch:
    Switch color
    Case green { execute block of commands }
    Case yellow { execute block of commands }
    Case blue { execute block of commands }
    Case pink { execute block of commands }
    
    Switch Statement
    Switch Statement

Using Switch statement for multiple conditions is so much easier to read! And if you have a lot of conditions to check, the execution should be also faster.


Edit

If Statement #

If statement is used to specify a block of commands to be executed if a condition (or multiple conditions) is true.
Commands under Else statement are executed instead if a condition is false.
You can think of it as If something is true, then do something, otherwise do something else.

By pressing the + button, you can add:

  • And - add another AND condition. All conditions must be true for the block to execute. If one of them is false, the whole IF statement will be also false and the ELSE block will be executed instead.
  • Or - add another OR condition. At least one of the conditions must be true for the block to execute. If all of them are false, the whole IF statement will be also false and the ELSE block will be executed instead.
Box Name Type Description
Value Left side of the comparison you want to evaluate.
Compare Dropdown menu Operator you wish to use to compare left and right side.
Value Whatever value you want to compare the left side with.


Edit

Switch Statement #

Switch statement is used to select one of many blocks of commands to be executed based on single condition.

Add one Switch statement and then nest Case Statement commands inside for each case.

Use the + button in the Case Statement command to add more than one condition to the same Switch Statement.

Use ‘default’ as a Case Statement text if you want to execute an action when no other cases meet the conditions.

Box Name Type Description
Value Expression to evaluate. Its result will be matched against each case you add.
  1. Add a new Switch Statement command
  2. Decide what expression you want to evaluate.
    For example, you would like to check the name of the viewer.
  3. Add a Case Statement for each choice.
    For example, add a Case Statement for wolbee, Melonax, Sebas and Cyanidesugar.
  4. Under each Case Statement, decide what commands you want to execute.
    For example, under Case Statement for wolbee, you want to play a specific sound effect. And under the Case Statement for Melonax, you want to play a different sound effect.
  5. (Optional) Add a default case to execute a block of commands if no Case Statement matches the expression.
    For example, if the viewer’s name isn’t wolbee, Melonax, Sebas or Cyanidesugar, play a default sound effect.
  6. Now every time the button is pressed, it will check the name of the viewer and execute one Case Statement command block matching their name (or execute the default block).

    Example of Switch Statement for viewer names
    Example of Switch Statement for viewer names


Edit

Case Statement #

Case Statement command can be only used inside Switch Statement.
Each case statement is a string the Switch Statement value will be compared with. If there’s a match, the associated block of code is executed.
Use default value if you want to execute an action when no other cases meet the condition.

Box Name Type Description
Text The case value. Must be a string. Does not allow variables.  


Edit

Re-enable #

Commands inside the Re-enable block will be repeated for X number of times with X milliseconds of delay between each repeat.
You can use Break command to break (stop) the loop if certain conditions are met.
You can create some really cool effects with this command.

Delays that are entered into the Delay field for commands within a Re-enable block will only work on the first loop. Delays will not work on any subsequent loops.

Box Name Type Description
Repeat Interval (ms) Int Delay between each re-enable. If set to 0, it will repeat indefinitely.
Amount Int How many times you want to re-enable the commands. Set to -1 for unlimited amount.


Edit

Repeat #

Instantly repeats all the commands inside the block for a specified number of times.
For example, this is useful for instantly retrieving multiple values from File: Load Value/String or from Array: Pull command.

You can use Wait Commands inside repeat.

Box Name Type Description
Repeat Amount Int (number) How many times you wish to repeat commands inside the block.


Edit

Break #

If the condition is true, this command will break out of a Repeat, Re-enable or Switch statement command, cancelling every command inside of the block if it hasn’t been executed yet.

Box Name Type Description
If Value Left side of the comparison you want to evaluate.
Compare Dropdown menu Operator you wish to use to compare left and right side.
Value Whatever you want to compare the left side with.


Edit

Exit #

If the condition is true, it terminates all the next button commands and prevents them from happening until the button is pressed again.

Box Name Type Description
If Value Left side of the comparison you want to evaluate.
Compare Type Dropdown menu Operator you wish to use to compare left and right side.
Value Whatever you want to compare the left side with.