""" MakeABranchStatement.cobra General syntax: branch WHAT on VALUE1 STATEMENTS1 on VALUE2 STATEMENTS2 [else STATEMENTS] branch WHAT on VALUE1, STATEMENT1 on VALUE2, STATEMENT2 [else, STATEMENT] Key rules of the branch statement: * What's being branched on must be an integer, character, enumeration or string. * The expressions being matched must be constants (ex: 0, 1, 'x', State.Off, "foo"). When a branch statement isn't feasible, you can possibly: * Make a class hierarchy and send a message to the object. * Use an `if-else` ladder (see MakeAnIfElseLadder.cobra) See also: MakeAnIfElseLadder.cobra, CheckInheritanceAndImplementation.cobra. """ class Program def main word = '' # This is the general syntax, below. Notice the keywords `branch` and # `on` as well as the indentation under each `on` part: 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' # In addition to `on`, you can put an `else` at the end: x = 2 branch x on 1 word = 'one' on 3 word = 'three' else word = 'else' assert word == 'else' # 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 colon (,) and putting the # statement on the same line, like so: x = 2 branch x on 1, word = 'one' on 3, word = 'three' else, word = 'else' assert word == 'else' # You can have more than one value for `on`: x = 2 branch x on 1 or 2 or 3, word = 'number' else, word = 'else' assert word == 'number' # You can also branch on enumerations, strings and characters.
How To Make A Branch Statement