Symbol Goto: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
<h1> {goto} &mdash; Branch to a line in a function &mdash; Keystroke ALT+] &mdash; Character 8594</h1>
<h1> {goto} &mdash; Branch to a line in a function &mdash; Keystroke ALT+] &mdash; Character 8594 or 0x2192</h1>
[[File:APLKB-Goto.png]]
[[File:APLKB-Goto.png]]
==Alternate Names==
==Alternate Names==
Line 6: Line 6:


==Usage==
==Usage==
==Example==
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.
==See Also==


===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:
:'''<tt> [5] {rightarrow}(A{lessthan}1)/22</tt>'''
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.


<br>As per the example from above, there are syntactically <u>three parts to a conditional branch</u>:
# Leftmost, the branch symbol itself, '''{rightarrow}'''
# Boolean test to perform, usually within parentheses, '''(A{lessthan}1)'''
# Statement number or label to go to if the test within parentheses is true(1), '''/22''' - note use of the reduction or slash symbol.


{{Language Toolbar}}
<br>The test inside the parentheses may be '''<u>simple</u>''', as per the above example.  The branch's boolean test may also be '''<u>compound</u>''', ref. the following:
[[Category: Mouse Group 1|G]]
<br>
[[Category:Symbols|G]]
:<tt> '''{rightarrow}((A{lessthan}1){downcaret}(A{morethan}10))/OR_Case'''</tt>
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 <u>either</u> test is true, the compound test evaluates to true - union of 2 tests(boolean 1 in either test results in a branch).<br><br>
Another compound or dual boolean test, this time a boolean AND:<br>
:<tt> '''{rightarrow}((A{lessthan}1){upcaret}(A{morethan}-10))/AND_NegativeCase'''</tt>
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{epsilon}(-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).
<br><br>
In setting up boolean '''go to''' tests also bear in mind use of APL's '''{tilde}''' or '''not''' symbol, thus:<br>
:'''<tt> [5] {rightarrow}({tilde}A{lessthan}1)/22</tt>'''
Thus APL would go to line number 22 only if A is greater than or equal to 1.
<br><br>
 
===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 <tt>:return</tt>
 
==Examples==
 
===Exit Function===
In the following example, [[function]] one calls function two, which exits part way through the function, returning to function 1.
<tt>
:{delta}one<br/>
[0] one<br/>
[1] 'This is one'<br/>
[2] two<br/>
[3] 'return to one from two'
:{delta}
 
:{delta}two
[0] two<br/>
[1] 'This is two'<br/>
[2] {goto}0<br/>
[3] 'Two Continues'
:{delta}
 
:one
This is one<br/>
This is two<br/>
return to one from two
</tt>
 
===Label Branch===
<tt>
:{delta}three<br/>
[0] three arg<br/>
[1] {goto}(arg=0)/dbz<br/>
[2] '3/',arg,'=',3/arg<br/>
[3] {goto}end<br/>
[4] dbz: 'Cannot divide by zero'<br/>
[5] end:
:{delta}
 
:three 0
Cannot divide by zero
 
:three 6
3/ 6= 0.5
</tt>
 
==See also==
<big>Another Example see: '''[[Control_Structures#Cases_Branching_Example_1|Control Structures - Branching Example]]'''</big>
<br><br>
<big>See also: '''[[Symbol_Tilde|Symbol Tilde or Not]] and [[Symbol_Slash|Symbol Slash or Reduce]]'''</big>
<br><br>
{{Article footer|1|G}}

Latest revision as of 02:52, 13 October 2019

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

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