Branch

BRANCH allows branching to one of multiple subroutines depending on the value of a specified variable. It’s the equivalent of a case statement. PicBasic Pro also has a BRANCH statement, as does mBasic. It looks like this:

BRANCH value, [label1, label2, label3...]

If value = 0, the program goes to label1. If value = 1, the program goes to label2. If value = 2, it goes to label3, and so forth.

 

Here’s a BS2 program illustrating BRANCH:

 

'branch.bs2
' program to illustrate branch statement
' this program takes a range of numbers from 0 to 3
' and branches to one of four different subroutines
' based on which number it's given
'
'hardware:
' rctime (analog) input on pin 7
' digital outputs on pins 8 to 11
' set up outs and ins:
output 8
output 9
output 10
output 11
input 7
'set up variables:
potVar var word        ' range from rctime
hitVar var byte        ' potVar divided into a range from 0 - 3
i var nib        ' variable for incrementing for-next loop
main:
    ' get the rctime value:    
    high 7
    pause 1
    rctime 7, 1, potVar
    'divide potVar by one fourth its maximum value:
    hitVar = potVar / 400
    debug ?hitVar
    'turn off all digial outs:
    for i = 8 to 11
        low i
    next
    
    ' go to one of four subroutines 
    ' depending on value of hitvar:
    branch hitvar, [one, two, three, four]
goto main
one:
    high 8
goto main
two:
    high 9
goto main
three:
    high 10
goto main
four:
    high 11
goto main