## Branching and labels

Traditionally, the APL right arrow '…' has been used to control execution in user-defined functions and operators. It can be used as a conditional or unconditional branch, and thus allows conditional execution and loops to be programmed. (Note that APLX alternatively allows you to control execution using structured-control keywords, which are preferable in many contexts).

The symbol … is usually followed by an integer scalar, vector, or label name which identifies the line to branch to. If the argument is a vector, the first element of the vector determines the line at which execution will continue, and subsequent elements are ignored. If the line number does not exist, the function terminates (often a line number of 0 is used for this purpose). If the argument is an empty vector, no branch is taken and execution continues at the next statement. Thus, conditional branches can be programmed by using a right argument which, at run-time, evaluates either to an integer scalar/vector, or to an empty vector.

A label is a name which is followed by a colon. It is placed at the start of the line which it identifies it. When the function is running, it is treated as a local variable whose value is the number of the line on which it is placed. It can thus be used directly as the argument of the right arrow.

A special case arises if no argument is given to the right arrow (a 'naked branch'). This terminates execution of the current function and of all functions which called it, removes them from the state indicator, and returns to desk-calculator mode. If the APL interpreter is already in desk-calculator mode, this will have the effect of removing the top function and all thouse down to the next function marked with an asterisk in the )SI display. A naked branch can also be used to end Œ input.

### Examples:

To branch back from line 10 to line 3:

          [10] …3

To branch unconditionally to a line labelled LAB2:

          [10] …LAB2
          [11] ...
          [12] LAB2:TOTAL„QTY×PRICE

To branch to a line labelled LAB2 only if LOOP has the value 10, by using an expression which evaluates to an empty vector if the condition is not true or to the label value if it is true:

          [10] …(LOOP=10)/LAB2
          [11] ...
          [12] LAB2:TOTAL„QTY×PRICE

To branch to one of several lines depending on the value of the variable INDEX:

          [6] …(CASE1 CASE2 CASE3)[INDEX]

To branch to one of several lines using a boolean vector to select which (execution will continue at the label corresponding to the first 1 in the vector. If there is none, a message will be displayed and the function will end):

          [6] …SELECT/(CASE1 CASE2 CASE3) ª 'No case applies' ª …0

Topic: APLX Help : Help on APL language : APL Fundamentals : Branching and labels

[ Previous | Next | Contents | Index ]