The branch statement is used to conditionally execute code where there are a range of execution options on a single variable value.


  branch <variable>
    on <value> [or <value>]...
    [on <value> [or <value>]...
        <statements> ]...

  branch <variable> 
    on <value>, <statement>
    on <value>, <statement> 
    [else, <statement>]

The value of variable is evaluated and the on clause matching the value is evaluated. If there is no match any else clause is evaluated

  • What's being branched on must be an integer, character, enumeration or string.
  • The expressions being matched ( the <value>s ) must be constants (e.g. 0, 1, c'x', State.Off, "foo").

The second form is for one line statements and allows the on value and statement to be on the same line. Multiple matching values can be specified separated by keyword or


    # Notice the keywords `branch` and 
    # `on` as well as the indentation under each `on` clause
    x = 2 
    branch x 
        on 1 
            isEven = false 
            word = 'one' 
        on 2 
            isEven = true
            word = 'two' 
        on 3 
            isEven = false 
            word = 'three' 
    assert isEven 
    assert word == 'two' 

If there is only one statement for the on or else and you want to tighten up your code, you can do so by using a comma (,) after the 'on' clause and putting the statement on the same line

    x = 'toof' 
    branch x 
        on 'woof', word = 'one' 
        on 'doof', word = 'three' 
        else, word = 'else' 
    assert word == 'else'

You can have more than one value for a single on clause

    x = 2 
    branch x 
        on 1 or 2 or 3, word = 'number' 
        else, word = 'else' 
    assert word == 'number'