Symbol Goto

From NARS2000
Jump to: navigation, search

→ — Branch to a line in a function — Keystroke ALT+] — Character 8594 or 0x2192

APLKB-Goto.png

Alternate Names

This symbol is also known as right arrow or APL text symbol {rightarrow}.

Usage

The goto symbol may be used as a conditional branch (branch based on a comparison) or an unconditional branch, or it may be used to exit a function. It is most often used in a function to transfer control, but can be used from the session manager to transfer to a line in a suspended function.

Conditional branch

The value to the right of the arrow is a boolean expression or non-negative number surrounded by parentheses, followed by a slash symbol and the line number to transfer to if the expression is true for a boolean expression or nonzero for a numeric value. A DOMAIN ERROR occurs if the numeric value is negative. In the following line 5 of a function:

[5] →(A<1)/22

Will branch to line 22 of the function if the value of A is less than 1. Execution would continue on line 6 if A was 1 or more.


As per the example from above, there are syntactically three parts to a conditional branch:

  1. Leftmost, the branch symbol itself,
  2. Boolean test to perform, usually within parentheses, (A<1)
  3. Statement number or label to go to if the test within parentheses is true(1), /22 - note use of the reduction or slash symbol.


The test inside the parentheses may be simple, as per the above example. The branch's boolean test may also be compound, ref. the following:

→((A<1)∨(A>10))/OR_Case

In this example if A is less than 1 OR A is greater than 10, the user function will branch to line label OR_Case: in the function. This occurs because if either test is true, the compound test evaluates to true - union of 2 tests(boolean 1 in either test results in a branch).

Another compound or dual boolean test, this time a boolean AND:

→((A<1)∧(A>-10))/AND_NegativeCase

Branching will occur only if BOTH tests evaluate to true. The user function will go to line label AND_NegativeCase: in the function only if (-10<A<1) or if A∊(-9 -8 -7 -6 -5 -4 -3 -2 -1 0} - intersection of 2 tests(boolean 1 if BOTH tests evaluate to true results in a branch).

In setting up boolean go to tests also bear in mind use of APL's or not symbol, thus:

[5] →(∼A<1)/22

Thus APL would go to line number 22 only if A is greater than or equal to 1.

Unconditional branch

The goto symbol is followed on the right either by the line number or a label. Execution continues in the function at that line (unless the line specified is 0, in which case the function exits.)

Exit Function

The goto symbol may be used to exit a function and return to the calling function (or return to the session manager if the function was called by the user typing its name) by using 0 as the line number to goto. This is the same as :return

Examples

Exit Function

In the following example, function one calls function two, which exits part way through the function, returning to function 1.

∆one

[0] one
[1] 'This is one'
[2] two
[3] 'return to one from two'

∆two

[0] two
[1] 'This is two'
[2] →0
[3] 'Two Continues'

one

This is one
This is two
return to one from two

Label Branch

∆three

[0] three arg
[1] →(arg=0)/dbz
[2] '3/',arg,'=',3/arg
[3] →end
[4] dbz: 'Cannot divide by zero'
[5] end:

three 0

Cannot divide by zero

three 6

3/ 6= 0.5

See also

Another Example see: Control Structures - Branching Example

See also: Symbol Tilde or Not and Symbol Slash or Reduce


See Also
System Commands System Variables and Functions Operators
Keyboard
A+S
Alt ¨ ¯ < > × ÷
Sh ~ ! @ # $ % ^ & * ( ) _ +
Key ` 1 2 3 4 5 6 7 8 9 0 - =
A+S
Alt ? π
Sh Q W E R T Y U I O P { } |
Key q w e r t y u i o p [ ] \
A+S §
Alt '
Sh A S D F G H J K L : "
Key a s d f g h j k l ; '
A+S
Alt
Sh Z X C V B N M < > ?
Key z x c v b n m , . /
NARS 2000 Lang
Tool
Bar
+ - × ÷ * ! ? |
= < >
~ § π .. ,
/ \ ¨ .
_ ¯ Ø b e g p r v x